forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes_test.go
92 lines (80 loc) · 2.36 KB
/
routes_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
tcclient "github.com/taskcluster/taskcluster/v47/clients/client-go"
)
func TestHttpRedirects(t *testing.T) {
// set up an upstream server that will return a redirect
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header()["Location"] = []string{"http://nosuch.example.com"}
w.WriteHeader(http.StatusSeeOther)
fmt.Fprintln(w, "{}")
}))
defer ts.Close()
// set up a routes object to test, using the test server as RootURL
routes := NewRoutes(
tcclient.Client{
Authenticate: true,
RootURL: ts.URL,
Credentials: &tcclient.Credentials{
ClientID: "some-client",
AccessToken: "doesn't-matter",
},
},
)
// create a fake request to the proxy
req, err := http.NewRequest(
"GET",
"http://localhost:60024/redirector/v1/redirect-me",
new(bytes.Buffer),
)
assert.NoError(t, err)
// see how it gets handled..
res := httptest.NewRecorder()
routes.ServeHTTP(res, req)
// it should have returned the 303 directly, along with its body
assert.Equal(t, 303, res.Code)
respBody, err := io.ReadAll(res.Body)
assert.NoError(t, err)
assert.Equal(t, "{}\n", string(respBody))
}
func TestNonCanonicalUrls(t *testing.T) {
// set up an upstream server that returns its path
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
fmt.Fprintf(w, "%s", r.URL)
}))
defer ts.Close()
// set up a routes object to test, using the test server as RootURL
routes := NewRoutes(
tcclient.Client{
Authenticate: true,
RootURL: ts.URL,
Credentials: &tcclient.Credentials{
ClientID: "some-client",
AccessToken: "doesn't-matter",
},
},
)
// create a fake request to the proxy
req, err := http.NewRequest(
"GET",
"http://localhost:60024/queue/v1/double//slash/encode1%2F/encode2%252F/encode3%25252F",
new(bytes.Buffer),
)
assert.NoError(t, err)
// see how it gets handled..
res := httptest.NewRecorder()
routes.ServeHTTP(res, req)
// it should have returned the path with `/api` but otherwise unchanged
assert.Equal(t, 200, res.Code)
respBody, err := io.ReadAll(res.Body)
assert.NoError(t, err)
assert.Equal(t, "/api/queue/v1/double//slash/encode1%2F/encode2%252F/encode3%25252F", string(respBody))
}