add tests for user and hostname atoms + moved server tests to server_test.go

This commit is contained in:
David Dworken
2022-04-10 17:38:20 -07:00
parent 71fa2bea97
commit 970e5d75db
2 changed files with 76 additions and 45 deletions

View File

@ -133,3 +133,40 @@ func TestUpdateReleaseVersion(t *testing.T) {
t.Fatalf("ReleaseVersion isn't as expected: %#v", ReleaseVersion)
}
}
func TestGithubRedirects(t *testing.T) {
// Set up
defer shared.BackupAndRestore(t)()
defer shared.RunTestServer(t)()
// Check the redirects
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Get("http://localhost:8080/download/hishtory-linux-amd64")
shared.Check(t, err)
if resp.StatusCode != 302 {
t.Fatalf("expected endpoint to return redirect")
}
locationHeader := resp.Header.Get("location")
if !strings.Contains(locationHeader, "https://github.com/ddworken/hishtory/releases/download/v") {
t.Fatalf("expected location header to point to github")
}
if !strings.HasSuffix(locationHeader, "/hishtory-linux-amd64") {
t.Fatalf("expected location header to point to binary")
}
// And retrieve it and check we can do that
resp, err = http.Get("http://localhost:8080/download/hishtory-linux-amd64")
shared.Check(t, err)
if resp.StatusCode != 200 {
t.Fatalf("didn't return a 200 status code, status_code=%d", resp.StatusCode)
}
respBody, err := ioutil.ReadAll(resp.Body)
shared.Check(t, err)
if len(respBody) < 5_000_000 {
t.Fatalf("response is too short to be a binary, resp=%d", len(respBody))
}
}