Debian Codegraph @

goproxytest/proxy_test.go

raw symbols SCIP
1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package goproxytest_test
6
7 import (
8 "encoding/json"
9 "io"
10 "net/http"
11 "path/filepath"
12 "testing"
13
14 "github.com/rogpeppe/go-internal/goproxytest"
15 "github.com/rogpeppe/go-internal/gotooltest"
16 "github.com/rogpeppe/go-internal/testscript"
17 )
18
19 func TestScripts(t *testing.T) {
20 srv := goproxytest.NewTestServer(t, filepath.Join("testdata", "mod"), "")
21 p := testscript.Params{
22 Dir: "testdata",
23 Setup: func(e *testscript.Env) error {
24 e.Vars = append(e.Vars,
25 "GOPROXY="+srv.URL,
26 "GONOSUMDB=*",
27 )
28 return nil
29 },
30 }
31 if err := gotooltest.Setup(&p); err != nil {
32 t.Fatal(err)
33 }
34 testscript.Run(t, p)
35 }
36
37 func TestSetup(t *testing.T) {
38 t.Run("with_proxy", func(t *testing.T) {
39 p := testscript.Params{
40 Files: []string{
41 "testdata/setup/with_proxy.txt",
42 },
43 }
44 if err := gotooltest.Setup(&p); err != nil {
45 t.Fatal(err)
46 }
47 goproxytest.Setup(&p)
48 testscript.Run(t, p)
49 })
50 t.Run("no_proxy", func(t *testing.T) {
51 p := testscript.Params{
52 Files: []string{
53 "testdata/setup/no_proxy.txt",
54 },
55 Setup: func(e *testscript.Env) error {
56 e.Vars = append(e.Vars, "GOPROXY=original")
57 return nil
58 },
59 }
60 if err := gotooltest.Setup(&p); err != nil {
61 t.Fatal(err)
62 }
63 goproxytest.Setup(&p)
64 testscript.Run(t, p)
65 })
66 t.Run("chained", func(t *testing.T) {
67 p := testscript.Params{
68 Files: []string{
69 "testdata/setup/chained.txt",
70 },
71 Setup: func(e *testscript.Env) error {
72 e.Vars = append(e.Vars, "CUSTOM=hello")
73 return nil
74 },
75 }
76 if err := gotooltest.Setup(&p); err != nil {
77 t.Fatal(err)
78 }
79 goproxytest.Setup(&p)
80 testscript.Run(t, p)
81 })
82 t.Run("overlay", func(t *testing.T) {
83 p := testscript.Params{
84 Files: []string{
85 "testdata/setup/overlay.txt",
86 },
87 }
88 if err := gotooltest.Setup(&p); err != nil {
89 t.Fatal(err)
90 }
91 goproxytest.Setup(&p)
92 testscript.Run(t, p)
93 })
94 }
95
96 func TestLatest(t *testing.T) {
97 srv := goproxytest.NewTestServer(t, filepath.Join("testdata", "mod"), "")
98
99 tests := []struct {
100 name string
101 module string
102 want string
103 }{
104 {
105 name: "release preferred over prerelease",
106 module: "fruit.com",
107 want: "v1.1.0",
108 },
109 {
110 name: "prerelease only",
111 module: "prerelease.example",
112 want: "v0.2.0-rc.1",
113 },
114 {
115 name: "unknown module",
116 module: "noexist.example",
117 want: "",
118 },
119 }
120 for _, test := range tests {
121 t.Run(test.name, func(t *testing.T) {
122 resp, err := http.Get(srv.URL + "/" + test.module + "/@latest")
123 if err != nil {
124 t.Fatal(err)
125 }
126 defer resp.Body.Close()
127 if test.want == "" {
128 if resp.StatusCode != 404 {
129 t.Fatalf("got status %d, want 404", resp.StatusCode)
130 }
131 return
132 }
133 if resp.StatusCode != 200 {
134 t.Fatalf("got status %d, want 200", resp.StatusCode)
135 }
136 body, err := io.ReadAll(resp.Body)
137 if err != nil {
138 t.Fatal(err)
139 }
140 var info struct {
141 Version string
142 }
143 if err := json.Unmarshal(body, &info); err != nil {
144 t.Fatalf("invalid JSON response: %v", err)
145 }
146 if info.Version != test.want {
147 t.Fatalf("got version %q, want %q", info.Version, test.want)
148 }
149 })
150 }
151 }