summaryrefslogtreecommitdiff
path: root/app/utilities_test.go
diff options
context:
space:
mode:
authorHorus_Arch2015-02-23 12:13:32 +0100
committerHorus_Arch2015-02-23 12:13:32 +0100
commit70d080b69a1bce3125d8d0b83e23775880241763 (patch)
tree6fac80f9291e361045098b2ce9eb0839652e6b42 /app/utilities_test.go
parent00b28812fb5ab68156ead5b45b66740a4d5ca688 (diff)
downloadstatuspage-70d080b69a1bce3125d8d0b83e23775880241763.tar.gz
Refactor and more unit tests.
Diffstat (limited to 'app/utilities_test.go')
-rw-r--r--app/utilities_test.go68
1 files changed, 65 insertions, 3 deletions
diff --git a/app/utilities_test.go b/app/utilities_test.go
index 5405309..22f1f5e 100644
--- a/app/utilities_test.go
+++ b/app/utilities_test.go
@@ -3,9 +3,37 @@
package main
import (
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "strings"
"testing"
)
+func TestHttpGet(t *testing.T) {
+ answer := "Fake webpage here."
+ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "text/html")
+ fmt.Fprintln(w, answer)
+ }))
+ defer ts.Close()
+
+ fakeUrl := ts.URL
+
+ resp, content, err := HttpGet(fakeUrl)
+ if err != nil {
+ t.Fatal("Error getting web page.")
+ }
+ if resp.StatusCode != 200 {
+ t.Fatal("Expecting 200 as status code.")
+ }
+ if strings.TrimSpace(content) != answer {
+ t.Log("We got this: ", content)
+ t.Log("We expected this: ", answer)
+ t.Fatal("Webpage returned wrong answer.")
+ }
+}
+
func TestMd5Hash(t *testing.T) {
hash := "f9d08276bc85d30d578e8883f3c7e843"
testHash := Md5Hash("md5hash")
@@ -16,9 +44,19 @@ func TestMd5Hash(t *testing.T) {
}
func TestRandomKey(t *testing.T) {
- key := RandomKey()
- if len(key) != 40 {
- t.Fatal("Expected a key with length of 40. Got %s.", key)
+ m := map[string]bool{}
+ var key string
+ for i := 0; i < 100; i++ {
+ key = RandomKey()
+ t.Log(key)
+ if m[key] {
+ t.Fatal("Key not random.")
+ } else {
+ m[key] = true
+ }
+ if len(key) != 40 {
+ t.Fatal("Expected a key with length of 40. Got %s.", key)
+ }
}
}
@@ -42,3 +80,27 @@ func TestPassword(t *testing.T) {
t.Fatal("Verifying empty password.")
}
}
+
+func BenchmarkMd5Hash(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ Md5Hash("md5hash")
+ }
+}
+
+func BenchmarkRandomKey(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ RandomKey()
+ }
+}
+
+func BenchmarkHashPassword(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ HashPassword("password")
+ }
+}
+
+func BenchmarkVerifyPassword(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ VerifyPassword("password", "$2a$10$OnsbG0Obaz2af3UkoQ9Jaeky3zfRi.0ZHCJC8DlWnbqbpaXEhWqYe")
+ }
+}