Skip to content

Commit

Permalink
feat: update tests
Browse files Browse the repository at this point in the history
* Update README.md

* fix: linting bugs

* fix: cleanup makefile

* fix: add more tests

* dont add empty headers

* fix lint
  • Loading branch information
Richard87 authored Oct 24, 2024
1 parent 58d3174 commit 95f5725
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,48 @@ func TestRun(t *testing.T) {
server := httptest.NewServer(router)
defer server.Close()

req, err := http.NewRequest(http.MethodGet, server.URL, nil)
body, err := sendRequest(server.URL, "hello-world-dev", "503")
require.NoError(t, err)
req.Header.Add("X-Namespace", "hello-world-dev")
req.Header.Add("X-Code", "503")
regularRequest, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer regularRequest.Body.Close()
assert.Contains(t, body, "Server error - Radix")

bytes, err := io.ReadAll(regularRequest.Body)
body, err = sendRequest(server.URL, "equinor-web-sites-dev", "503")
require.NoError(t, err)
assert.Contains(t, string(bytes), "Server error - Radix")
assert.Contains(t, body, "Something went wrong - Equinor")

req, err = http.NewRequest(http.MethodGet, server.URL, nil)
body, err = sendRequest(server.URL, "", "")
require.NoError(t, err)
req.Header.Add("X-Namespace", "equinor-web-sites-dev")
req.Header.Add("X-Code", "503")
equinorRequst, err := http.DefaultClient.Do(req)
assert.Contains(t, body, "Server error - Radix")

body, err = sendRequest(server.URL+"/hello-world", "", "")
require.NoError(t, err)
defer equinorRequst.Body.Close()
assert.Contains(t, body, "Server error - Radix")

bytes, err = io.ReadAll(equinorRequst.Body)
body, err = sendRequest(server.URL+"/hello-world", "default", "503")
require.NoError(t, err)
assert.Contains(t, string(bytes), "Something went wrong - Equinor")
assert.Contains(t, body, "Server error - Radix")
}

func sendRequest(url, namespace, code string) (string, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return "", err
}

if namespace != "" {
req.Header.Add("X-Namespace", namespace)
}
if code != "" {
req.Header.Add("X-Code", code)
}
regularRequest, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer regularRequest.Body.Close()

bytes, err := io.ReadAll(regularRequest.Body)
if err != nil {
return "", err
}
return string(bytes), nil
}

0 comments on commit 95f5725

Please sign in to comment.