forked from inno-devops-labs/S24-core-course-labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
// TestCatFact tests that `catFact` loads non-empty facts | ||
// without errors and that those facts are different | ||
func TestCatFact(t *testing.T) { | ||
const TRIES = 5 | ||
|
||
options := map[string]int{} | ||
|
||
for i := 0; i < TRIES; i++ { | ||
s, e := catFact() | ||
if e != nil { | ||
t.Fatal("Error quering a cat fact", e) | ||
} else if s == "" { | ||
t.Fatal("No error occurred but `catFact` returned" + | ||
"an empty string") | ||
} | ||
|
||
options[s] += 1 | ||
} | ||
|
||
// Tests are repeating | ||
t.Logf("Out of %d cat facts, %d are unique", TRIES, len(options)) | ||
if len(options) <= (1+TRIES)/2 { | ||
t.FailNow() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
) | ||
|
||
// TestFactLoads tests that the handler returns something that | ||
// is neither empty nor an error | ||
func TestFactLoads(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
|
||
handler(w, nil) | ||
resp := w.Result() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
t.Fatal("Server responded with exit code", w.Code) | ||
} | ||
|
||
buf, err := io.ReadAll(w.Body) | ||
body := string(buf) | ||
if err != nil { | ||
t.Fatal("Failed to read response body") | ||
} | ||
if len(buf) == 0 { | ||
t.Fatal("Response body is empty") | ||
} | ||
if strings.Contains(body, "Fail") && strings.Contains(body, ":(") { | ||
t.Fatal("An error occurred while retreiving cat fact") | ||
} | ||
} |