Release / build-and-push (push) Skipped
Build / build-and-push (push) Successful in 1m7s
A row with a status code outside 3xx (e.g. "0" from a typo) was accepted
at load and then panicked net/http's WriteHeader on the first matching
request. ReadCsvRedirects now rejects anything that isn't 300-399, with a
line number in the message, and reports a non-numeric code clearly.
Also defer reader.Close() (the file was left open for the process
lifetime) and drop the unreachable "row == nil" check.
Adds internal/{csv,redirect,http}_test.go covering CSV parsing (valid,
reordered headings, header-only, missing heading, non-numeric and non-3xx
status, ragged row, missing file), Redirects.Find / Redirect.Match, and
ServeHTTP (redirect, 404, query string dropped).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
package internal
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
func newTestServer(rs Redirects) *HttpServer {
|
|
return &HttpServer{
|
|
CLI: &CLI{},
|
|
Log: zerolog.Nop(),
|
|
Redirects: rs,
|
|
}
|
|
}
|
|
|
|
func TestServeHTTP(t *testing.T) {
|
|
srv := newTestServer(Redirects{
|
|
{From: "/old", To: "https://example.com/new", StatusCode: 301},
|
|
})
|
|
|
|
t.Run("known path redirects with Location", func(t *testing.T) {
|
|
rec := httptest.NewRecorder()
|
|
srv.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/old", nil))
|
|
|
|
if rec.Code != 301 {
|
|
t.Errorf("status = %d, want 301", rec.Code)
|
|
}
|
|
if loc := rec.Header().Get("Location"); loc != "https://example.com/new" {
|
|
t.Errorf("Location = %q", loc)
|
|
}
|
|
})
|
|
|
|
t.Run("unknown path is 404 with no Location", func(t *testing.T) {
|
|
rec := httptest.NewRecorder()
|
|
srv.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/nope", nil))
|
|
|
|
if rec.Code != http.StatusNotFound {
|
|
t.Errorf("status = %d, want 404", rec.Code)
|
|
}
|
|
if loc := rec.Header().Get("Location"); loc != "" {
|
|
t.Errorf("Location = %q, want empty", loc)
|
|
}
|
|
})
|
|
|
|
t.Run("query string is dropped (current behaviour)", func(t *testing.T) {
|
|
rec := httptest.NewRecorder()
|
|
srv.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/old?ref=x", nil))
|
|
|
|
if loc := rec.Header().Get("Location"); loc != "https://example.com/new" {
|
|
t.Errorf("Location = %q", loc)
|
|
}
|
|
})
|
|
}
|