/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ package main import ( "bytes" "context" "fmt" "net/http" "os" "strings" "testing" ) var srv *Server func TestMain(m *testing.M) { ctx := context.Background() srv = NewServer(ctx) ret := m.Run() os.Exit(ret) } func TestGoProxyProtocol(t *testing.T) { t.Run( "Returns expected JSON on request @v/$version.info for existing module", func(t *testing.T) { expected := `{"version": "v2.0.0", ` + `"timestamp": "2023-03-09T01:01:35Z"}` + "\n" module := "idio.link/go/netaddr/v2" version := "v2.0.0" req, _ := http.NewRequest( "GET", fmt.Sprintf("https://%s/@v/%s.info", module, version), nil, ) w := NewFakeWriter() srv.handleGoGet()(w, req) actual := w.buf.String() if actual != expected { t.Errorf("\nexpected:\n%#v\n\nactual:\n%#v", expected, actual) } }, ) t.Run( "Receive expected HTML on request go-get for existing module", func(t *testing.T) { expected := `` + `` + `` + `` + `` + `` + `` + `go get https://idio.link/go/netaddr` + `` + `` module := "idio.link/go/netaddr" reqURI := fmt.Sprintf("https://%s/v2?go-get=1", module) req, _ := http.NewRequest("GET", reqURI, nil) w := NewFakeWriter() srv.handleGoGet()(w, req) actual := w.buf.String() if strings.Compare(actual, expected) != 0 { t.Errorf("expected:\n%s\nactual:\n%s", expected, actual) } }, ) t.Run( "Receive another expected HTML on request go-get for existing module", func(t *testing.T) { expected := `` + `` + `` + `` + `` + `` + `` + `go get https://idio.link/go/netaddr` + `` + `` module := "idio.link/go/netaddr" reqURI := fmt.Sprintf("https://%s?go-get=1", module) req, _ := http.NewRequest("GET", reqURI, nil) w := NewFakeWriter() srv.handleGoGet()(w, req) actual := w.buf.String() if strings.Compare(actual, expected) != 0 { t.Errorf("expected:\n%s\nactual:\n%s", expected, actual) } }, ) t.Run( "Receive list of module versions on request @v/list for existing module", func(t *testing.T) { expected := `v0.0.1 v1.0.0 v1.1.0 v1.2.0 v1.2.1 v1.3.0 v1.4.0 v1.5.0 v1.6.0 ` module := "idio.link/go/netaddr" reqURI := fmt.Sprintf("https://%s/@v/list", module) req, _ := http.NewRequest("GET", reqURI, nil) w := NewFakeWriter() srv.handleGoGet()(w, req) actual := w.buf.String() if strings.Compare(actual, expected) != 0 { t.Errorf("expected:\n%s\nactual:\n%s", expected, actual) } }, ) t.Run( "Receive list of v2 module versions on request @v/list for existing module", func(t *testing.T) { expected := `v2.0.0 v2.0.1 v2.1.0 v2.1.1 ` module := "idio.link/go/netaddr/v2" reqURI := fmt.Sprintf("https://%s/@v/list", module) req, _ := http.NewRequest("GET", reqURI, nil) w := NewFakeWriter() srv.handleGoGet()(w, req) actual := w.buf.String() if strings.Compare(actual, expected) != 0 { t.Errorf("expected:\n%s\nactual:\n%s", expected, actual) } }, ) } func NewFakeWriter() *FakeWriter { buf := make([]byte, 0, 2048) return &FakeWriter{ buf: bytes.NewBuffer(buf), } } type FakeWriter struct { buf *bytes.Buffer status int } func (w *FakeWriter) Header() http.Header { h := make(map[string][]string) return h } func (w *FakeWriter) Write(b []byte) (int, error) { return w.buf.Write(b) } func (w *FakeWriter) WriteHeader(status int) { w.status = status }