From 41d23c22df853b0bdf35e2d0988c8d4c4281d42f Mon Sep 17 00:00:00 2001 From: Grégoire Duchêne Date: Sun, 19 Jun 2022 13:31:49 +0100 Subject: Move redirection logic to a separate package MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also, add an optional ‘-c’ flag to pass the path to a configuration file that can be used to specify several matching patterns and replacements. --- resp_test.go | 70 ------------------------------------------------------------ 1 file changed, 70 deletions(-) delete mode 100644 resp_test.go (limited to 'resp_test.go') diff --git a/resp_test.go b/resp_test.go deleted file mode 100644 index 7e01946..0000000 --- a/resp_test.go +++ /dev/null @@ -1,70 +0,0 @@ -// SPDX-FileCopyrightText: © 2019 Grégoire Duchêne -// SPDX-License-Identifier: ISC - -package main - -import ( - "io" - "net/http" - "net/http/httptest" - "regexp" - "testing" -) - -func TestRedirector_ServeHTTP(t *testing.T) { - r := &redirector{regexp.MustCompile(`src\.example\.com/x`), "https://example.com/git", "git"} - - t.Run("NotFound", func(t *testing.T) { - req := httptest.NewRequest(http.MethodGet, "https://example.com/foo", nil) - w := httptest.NewRecorder() - r.ServeHTTP(w, req) - - resp := w.Result() - if http.StatusNotFound != resp.StatusCode { - t.Errorf("expected %d, got %d", http.StatusNotFound, resp.StatusCode) - } - }) - - t.Run("GoVisit", func(t *testing.T) { - req := httptest.NewRequest(http.MethodGet, "https://src.example.com/x/foo?go-get=1", nil) - w := httptest.NewRecorder() - r.ServeHTTP(w, req) - - resp := w.Result() - if http.StatusOK != resp.StatusCode { - t.Errorf("expected %d, got %d", http.StatusFound, resp.StatusCode) - } - body, err := io.ReadAll(resp.Body) - if err != nil { - t.Error(err) - t.FailNow() - } - expected := ` - -go-import-redirect -` - if string(body) != expected { - t.Errorf("expected\n---\n%s\n---\ngot\n---\n%s\n---", expected, string(body)) - } - if hdr := resp.Header.Get("Location"); hdr != "" { - t.Error("expected empty Location header") - } - }) - - t.Run("UserVisit", func(t *testing.T) { - req := httptest.NewRequest(http.MethodGet, "https://src.example.com/x/foo", nil) - w := httptest.NewRecorder() - r.ServeHTTP(w, req) - - resp := w.Result() - if http.StatusFound != resp.StatusCode { - t.Errorf("expected %d, got %d", http.StatusFound, resp.StatusCode) - } - if resp.ContentLength > 0 { - t.Error("expected empty body") - } - if hdr := resp.Header.Get("Location"); hdr != "https://pkg.go.dev/src.example.com/x/foo" { - t.Errorf("expected %q, got %q", "https://pkg.go.dev/src.example.com/x/foo", hdr) - } - }) -} -- cgit v1.2.3-70-g09d2