Short-Link Redirect Testing in CI: Contract Tests for Destinations, TLS, and UTM Preservation
CI/CDtestingredirectsTLSUTMdeveloper workflows

Short-Link Redirect Testing in CI: Contract Tests for Destinations, TLS, and UTM Preservation

GGoog Labs Editorial Team
2026-11-20
8 min read

Build CI contract tests for branded short links that verify redirects, TLS, encoded URLs, UTM preservation, and safe deployment gates.

Short links are small interfaces with production consequences: a changed destination, DNS record, certificate, or query-string rule can break a campaign or send users somewhere unexpected. This guide shows how to build redirect contract tests in CI so branded short links are checked automatically for status codes, redirect chains, TLS, encoded URLs, and UTM preservation before changes reach production.

Overview

A redirect contract test defines what a short link must do. Instead of checking only that https://go.example/spring returns a response, the test records the expected behavior: the initial status, the Location header, the number of hops, the final URL, and the security properties of each hop.

This approach is useful whether your custom short domain runs on a managed redirect service, an edge worker, or a self-hosted routing API. The implementation can change, but the contract remains stable. It also complements, rather than replaces, uptime monitoring. A monitor answers “is this link responding now?” A contract test answers “does this link still respond in the approved way after a deployment or configuration change?”

For most short-link systems, test three layers:

  • Transport: DNS resolves, HTTPS is valid, and the endpoint is reachable.
  • Routing: the expected status and destination are returned without an accidental chain or open redirect.
  • Tracking: approved path, query-string, and UTM values survive the redirect exactly as intended.

Keep expected destinations in version-controlled fixtures. This makes a routing change reviewable and gives developers a clear failure message when a short link contract changes. For related routing safeguards, see open redirect prevention for short-link systems.

Checklist by scenario

1. Build a staging fixture

Start with a small, representative set of branded links rather than every link in the database. Include one stable production-like route, one campaign URL with UTM parameters, one destination containing encoded characters, and one route designed to fail safely.

A fixture can be JSON, YAML, or a database export. The important fields are the short URL, expected status, allowed destination, and whether a query string is required.

[
  {
    "name": "homepage",
    "shortUrl": "https://go.example/home",
    "expectedStatus": 302,
    "expectedFinalUrl": "https://www.example.com/"
  },
  {
    "name": "campaign",
    "shortUrl": "https://go.example/spring",
    "expectedStatus": 302,
    "expectedFinalUrl": "https://www.example.com/spring",
    "requiredQuery": {
      "utm_source": "newsletter",
      "utm_medium": "email",
      "utm_campaign": "spring"
    }
  },
  {
    "name": "encoded-resource",
    "shortUrl": "https://go.example/docs",
    "expectedStatus": 301,
    "expectedFinalUrl": "https://www.example.com/docs/a%2Fb"
  }
]

Use a staging vanity short domain or isolated routes where possible. Staging tests should exercise the same routing code and DNS automation path as production, but they should not depend on a live marketing campaign or a mutable third-party page.

2. Assert the first response

Use an HTTP client that can disable automatic redirect following. The first response is where you verify the contract’s status and Location header. A test that follows redirects immediately can hide an incorrect first hop.

CheckRecommended assertionWhy it matters
StatusAllow only the status codes your routing policy defines, commonly 301, 302, 307, or 308.Prevents silent changes in caching or method behavior.
LocationRequire an absolute HTTPS URL or an explicitly approved relative form.Catches missing, malformed, or insecure destinations.
Hop countSet a maximum, often one for a short-link endpoint.Reduces latency and prevents hidden routing loops.
Final URLCompare scheme, host, path, and approved query parameters.Confirms the user reaches the intended resource.
TLSUse normal certificate and hostname validation on every HTTPS hop.Detects certificate, hostname, or trust-chain problems.

Do not make HEAD your only test method. Some origin servers and routing layers handle HEAD differently from GET. A practical suite can use a lightweight GET with redirects disabled, then separately test browser-like behavior where it matters.

3. Test the complete chain

After asserting the first response, follow the chain with a strict hop limit. Record every URL and status. Fail if a supposedly direct link becomes short domain → tracking host → HTTP destination → final page, unless that chain is explicitly documented.

Require HTTPS for the short URL and for each destination that your policy controls. Keep certificate verification enabled; do not “fix” a failing TLS test by turning off verification in CI. If a test environment uses an internal certificate authority, install that authority in the test runner instead of bypassing validation.

4. Verify encoded URLs and query strings

URL parsing is a common source of redirect regressions. Test destinations containing spaces, Unicode characters, reserved characters, encoded slashes, and existing query parameters. Compare parsed URL components rather than relying only on string equality, but preserve the distinction between encoded and decoded values when it affects the destination.

For UTM tracking, define whether the short-link service appends, replaces, or preserves parameters. A safe contract might require utm_source, utm_medium, and utm_campaign, while allowing unrelated parameters to remain unchanged. Do not accept arbitrary parameter injection if it can alter a destination or bypass routing rules.

5. Add the deployment gate

Run contract tests on pull requests that change routing code, redirect rules, DNS automation, domain configuration, analytics handling, or campaign fixtures. Run them again after deployment against the staging endpoint, then against production after an approved release step.

A minimal GitHub Actions shape looks like this:

name: redirect-contracts

on:
  pull_request:
    paths:
      - 'redirects/**'
      - 'dns/**'
      - 'tests/redirects/**'
  workflow_dispatch:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - run: npm ci
      - run: npm test -- redirect-contracts
        env:
          REDIRECT_BASE_URL: https://staging-go.example

Provider syntax and action versions should be reviewed as part of your normal maintenance process. The essential deployment rule is provider-independent: a failed redirect contract blocks promotion unless an authorized change to the fixture explains the new behavior.

If routes are created or updated through a short link API, test both the API response and the public URL. A successful API write does not prove that edge caches, DNS, certificates, or routing workers serve the new record correctly.

const response = await fetch(`${process.env.API_URL}/links/spring`, {
  headers: { Authorization: `Bearer ${process.env.TEST_TOKEN}` }
});

if (!response.ok) throw new Error(`API returned ${response.status}`);

const link = await response.json();
if (link.destination !== 'https://www.example.com/spring') {
  throw new Error('API destination does not match the fixture');
}

Use disposable identifiers or a dedicated staging namespace. Never let a CI test overwrite a live campaign route without an explicit safety control.

What to double-check

  • Status semantics: Choose redirect codes deliberately. Permanent and temporary redirects can be cached differently, and 307 or 308 preserve the request method more strictly than 302 or 301 in some clients.
  • Destination allowlists: Compare the final host and path against approved patterns. Avoid tests that merely check that a response is “not empty.”
  • Query preservation: Confirm parameter order only when order matters to the receiving application. Otherwise compare parsed key-value pairs, including duplicate-parameter behavior where relevant.
  • Fragments: URL fragments are handled by the browser and are not sent to the server. Test them with a browser or URL-construction check rather than expecting them in an HTTP response.
  • DNS and certificates: Verify the hostname used by the public short URL, not only an origin hostname. Include certificate hostname matching, expiry handling, and the intended DNS record in the release checklist.
  • Cache behavior: After a destination update, test from an environment that reflects the intended cache layer. A passing origin test may not represent what users receive from an edge cache.
  • Analytics privacy: Assert only the tracking fields you need. Avoid collecting full query strings in CI logs when they may contain personal or sensitive data. See the guide to privacy-friendly short-link analytics.

Common mistakes

Following redirects without inspection. This hides an incorrect status, a dangerous intermediate host, or an accidental loop. Inspect the first response and every subsequent hop.

Testing only the homepage. A homepage route rarely exercises encoded paths, UTM handling, QR-code destinations, or legacy links. Keep fixtures small but varied.

Using overly broad string matching. A check for example.com could accept example.com.attacker.test. Parse the URL and compare the hostname exactly or against a carefully defined allowlist.

Disabling TLS verification in CI. This removes one of the checks the pipeline is meant to provide. Correct the certificate, trust store, or staging hostname instead.

Assuming a 200 response is always correct. A redirect endpoint may return a 200 error page, a login screen, or a fallback destination. Assert the route contract, not just availability.

Making fixtures too brittle. Full-page content assertions create noise when the destination changes legitimately. Test the URL components and tracking rules that your short-link system owns, and use separate destination tests for application content.

Changing fixtures without review. Updating the expected destination can turn a broken route into a passing test. Require an explanation, code review, and—where appropriate—a separate approval for production destination changes.

When to revisit

Review redirect contracts before seasonal planning cycles, campaign launches, QR-code distribution, and domain or provider migrations. A printed QR code may remain in circulation long after its campaign changes, so its short URL deserves a durable test fixture; see QR-code destination and scan-tracking guidance.

Revisit the suite whenever you change DNS automation, edge workers, redirect API behavior, certificate management, analytics parameters, URL encoding rules, or HTTP-client and browser tooling. Also review it after an incident involving a broken link, unexpected redirect chain, stale cache, or incorrect campaign attribution.

As a practical final pass, confirm that:

  1. Every important branded short link has a version-controlled fixture.
  2. The first response, complete chain, final URL, and TLS behavior are asserted.
  3. Encoded paths and UTM preservation have explicit test cases.
  4. Open redirects and unapproved destination hosts fail safely.
  5. Pull-request and post-deployment checks use the intended staging and production endpoints.
  6. Failure logs contain enough URL context to debug without exposing sensitive query data.

Once these checks are part of the deployment gate, a short link becomes a tested API contract rather than an assumption hidden in DNS or routing configuration. That makes destination changes safer, provider migrations easier to review, and branded links more dependable over their entire lifecycle. For operational follow-through, pair contract tests with uptime and redirect-latency monitoring.

Related Topics

#CI/CD#testing#redirects#TLS#UTM#developer workflows
G

Goog Labs Editorial Team

Developer Infrastructure Editors

Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.