|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/julienschmidt/httprouter" |
| 10 | + "github.com/suyashkumar/getbin/releases" |
| 11 | +) |
| 12 | + |
| 13 | +var errStopRedirect = errors.New("an error to stop following redirects") |
| 14 | +var defaultGithubAPIResponse = []byte(`[{ |
| 15 | + "assets": [ |
| 16 | + {"browser_download_url": "http://localhost/some-file-darwin-x86.tar.gz", "content_type": "application/x-gzip"}, |
| 17 | + {"browser_download_url": "http://localhost/some-file-windows-x86.tar.gz", "content_type": "application/x-gzip"}, |
| 18 | + {"browser_download_url": "http://localhost/some-file-linux-x86.tar.gz", "content_type": "application/x-gzip"} |
| 19 | + ] |
| 20 | + }]`) |
| 21 | + |
| 22 | +func TestDownload_Redirect(t *testing.T) { |
| 23 | + cases := []struct { |
| 24 | + name string |
| 25 | + requestPath string |
| 26 | + userAgent string |
| 27 | + githubAPIResponse []byte |
| 28 | + wantRedirect string |
| 29 | + }{ |
| 30 | + { |
| 31 | + name: "darwin option", |
| 32 | + requestPath: "/username/repo?os=darwin", |
| 33 | + githubAPIResponse: defaultGithubAPIResponse, |
| 34 | + wantRedirect: "http://localhost/some-file-darwin-x86.tar.gz", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "darwin user-agent", |
| 38 | + requestPath: "/username/repo", |
| 39 | + userAgent: "darwin user agent", |
| 40 | + githubAPIResponse: defaultGithubAPIResponse, |
| 41 | + wantRedirect: "http://localhost/some-file-darwin-x86.tar.gz", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "linux option", |
| 45 | + requestPath: "/username/repo?os=linux", |
| 46 | + githubAPIResponse: defaultGithubAPIResponse, |
| 47 | + wantRedirect: "http://localhost/some-file-linux-x86.tar.gz", |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "linux user-agent", |
| 51 | + requestPath: "/username/repo", |
| 52 | + userAgent: "linux user agent", |
| 53 | + githubAPIResponse: defaultGithubAPIResponse, |
| 54 | + wantRedirect: "http://localhost/some-file-linux-x86.tar.gz", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "windows option", |
| 58 | + requestPath: "/username/repo?os=windows", |
| 59 | + githubAPIResponse: defaultGithubAPIResponse, |
| 60 | + wantRedirect: "http://localhost/some-file-windows-x86.tar.gz", |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "windows user-agent", |
| 64 | + requestPath: "/username/repo", |
| 65 | + userAgent: "windows user agent", |
| 66 | + githubAPIResponse: defaultGithubAPIResponse, |
| 67 | + wantRedirect: "http://localhost/some-file-windows-x86.tar.gz", |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + for _, tc := range cases { |
| 72 | + tc := tc |
| 73 | + t.Run(tc.name, func(t *testing.T) { |
| 74 | + wantRequestURL := "/repos/username/repo/releases" |
| 75 | + |
| 76 | + // Setup fake GitHub server. |
| 77 | + githubServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 78 | + if req.URL.String() != wantRequestURL { |
| 79 | + t.Errorf("unexpected request URL. got: %v, want: %v", req.URL.String(), wantRequestURL) |
| 80 | + w.WriteHeader(http.StatusBadRequest) |
| 81 | + return |
| 82 | + } |
| 83 | + w.Write(tc.githubAPIResponse) |
| 84 | + })) |
| 85 | + defer githubServer.Close() |
| 86 | + tempSetGithubAPIBase(t, githubServer.URL) |
| 87 | + |
| 88 | + // Setup test getbin server to wrap the Download handler. |
| 89 | + router := httprouter.New() |
| 90 | + router.GET("/:username/:repo", Download) |
| 91 | + server := httptest.NewServer(router) |
| 92 | + defer server.Close() |
| 93 | + |
| 94 | + // Make test request and client to getbin. |
| 95 | + req, err := http.NewRequest(http.MethodGet, server.URL+tc.requestPath, nil) |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("unable to make GET request to getbin server: %v", err) |
| 98 | + } |
| 99 | + req.Header.Set("User-Agent", tc.userAgent) |
| 100 | + |
| 101 | + cl := &http.Client{CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 102 | + // Need to return an error to stop the client from auto |
| 103 | + // redirecting, since we want to inspect the redirect. |
| 104 | + return errStopRedirect |
| 105 | + }} |
| 106 | + |
| 107 | + res, err := cl.Do(req) |
| 108 | + if !errors.Is(err, errStopRedirect) { |
| 109 | + t.Errorf("Unexpected error when making getbin request: %v", err) |
| 110 | + } |
| 111 | + |
| 112 | + if res.StatusCode != http.StatusMovedPermanently { |
| 113 | + t.Errorf("unexpected StatusCode in response. got: %v, want: %v", res.StatusCode, http.StatusMovedPermanently) |
| 114 | + } |
| 115 | + if got := res.Header.Get("Location"); got != tc.wantRedirect { |
| 116 | + t.Errorf("unexpected redirect in response. got: %v, want: %v", got, tc.wantRedirect) |
| 117 | + } |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func tempSetGithubAPIBase(t *testing.T, newAPIBase string) { |
| 123 | + origVal := releases.GithubAPIBase |
| 124 | + releases.GithubAPIBase = newAPIBase |
| 125 | + t.Cleanup(func() { |
| 126 | + releases.GithubAPIBase = origVal |
| 127 | + }) |
| 128 | +} |
0 commit comments