package main import ( "net/http" "net/http/httptest" "net/url" "strings" "testing" ) func TestIndexHandler(t *testing.T) { request, err := http.NewRequest("GET", "/", nil) if err != nil { t.Log("Error creating new http request. (/)", err) } response := httptest.NewRecorder() IndexHandler(response, request) if response.Code != 200 { t.Log(response.Code) t.Fatal("Expected 200 as status code.") } request, err = http.NewRequest("GET", "/", nil) if err != nil { t.Log("Error creating new http request. (/)", err) } response = httptest.NewRecorder() cookie := http.Cookie{Name: "lang", Value: "de"} request.AddCookie(&cookie) IndexHandler(response, request) if response.Code != 200 { t.Log(response.Code) t.Fatal("Expected 200 as status code. Language: de") } } func BenchmarkIndexHandler(b *testing.B) { b.StopTimer() request, _ := http.NewRequest("GET", "/", nil) response := httptest.NewRecorder() b.StartTimer() for i := 0; i < b.N; i++ { IndexHandler(response, request) } } func TestRegisterHandler(t *testing.T) { request, err := http.NewRequest("GET", "/register", nil) if err != nil { t.Log("Error creating new http request. (/register)", err) } response := httptest.NewRecorder() RegisterHandler(response, request) if response.Code != 200 { t.Log(response.Code) t.Fatal("Expected 200 as status code.") } request, err = http.NewRequest("GET", "/register", nil) if err != nil { t.Log("Error creating new http request. (/register)", err) } response = httptest.NewRecorder() cookie := http.Cookie{Name: "lang", Value: "de"} request.AddCookie(&cookie) RegisterHandler(response, request) if response.Code != 200 { t.Log(response.Code) t.Fatal("Expected 200 as status code. Language: de") } } func BenchmarkRegisterHandler(b *testing.B) { b.StopTimer() request, _ := http.NewRequest("GET", "/register", nil) response := httptest.NewRecorder() b.StartTimer() for i := 0; i < b.N; i++ { RegisterHandler(response, request) } } func TestCreateNewEntryHandler(t *testing.T) { form := url.Values{} form.Set("Email", "createNewEntry@localhost") form.Set("ConfirmEmail", "createNewEntry@localhost") form.Set("Password", "Password") form.Set("ConfirmPassword", "Password") request, err := http.NewRequest("POST", "/create", strings.NewReader(form.Encode())) if err != nil { t.Log("Error creating new http request. (/create)", err) } request.Header.Add("Content-Type", "application/x-www-form-urlencoded") response := httptest.NewRecorder() CreateNewEntryHandler(response, request) if response.Code != 302 { t.Fatal("Expected 302 as status code.", response.Code) } flash := Flash{} session, err := store.Get(request, "_SID") if err != nil { t.Log(err) } flash.Error = session.Flashes("error") flash.Success = session.Flashes("success") if len(flash.Error) > 0 { for _, v := range flash.Error { t.Fatal(v) } t.Fatal("Got error message.") } if len(flash.Success) == 0 { t.Fatal("Expected success message.") } t.Log(flash.Success) } func TestAboutHandler(t *testing.T) { request, err := http.NewRequest("GET", "/about", nil) if err != nil { t.Log("Error creating new http request. (/about)", err) } response := httptest.NewRecorder() AboutHandler(response, request) if response.Code != 200 { t.Log(response.Code) t.Fatal("Expected 200 as status code.") } request, err = http.NewRequest("GET", "/about", nil) if err != nil { t.Log("Error creating new http request. (/about)", err) } response = httptest.NewRecorder() cookie := http.Cookie{Name: "lang", Value: "de"} request.AddCookie(&cookie) AboutHandler(response, request) if response.Code != 200 { t.Log(response.Code) t.Fatal("Expected 200 as status code. Language: de") } } func BenchmarkAboutHandler(b *testing.B) { b.StopTimer() request, _ := http.NewRequest("GET", "/about", nil) response := httptest.NewRecorder() b.StartTimer() for i := 0; i < b.N; i++ { AboutHandler(response, request) } } func TestServerHandler(t *testing.T) { request, err := http.NewRequest("GET", "/server", nil) if err != nil { t.Log("Error creating new http request. (/server)", err) } response := httptest.NewRecorder() ServerHandler(response, request) if response.Code != 200 { t.Log(response.Code) t.Fatal("Expected 200 as status code.") } request, err = http.NewRequest("GET", "/server", nil) if err != nil { t.Log("Error creating new http request. (/server)", err) } response = httptest.NewRecorder() cookie := http.Cookie{Name: "lang", Value: "de"} request.AddCookie(&cookie) ServerHandler(response, request) if response.Code != 200 { t.Log(response.Code) t.Fatal("Expected 200 as status code. Language: de") } } func BenchmarkServerHandler(b *testing.B) { b.StopTimer() request, _ := http.NewRequest("GET", "/server", nil) response := httptest.NewRecorder() b.StartTimer() for i := 0; i < b.N; i++ { ServerHandler(response, request) } } func TestHowtoHandler(t *testing.T) { request, err := http.NewRequest("GET", "/howto", nil) if err != nil { t.Log("Error creating new http request. (/howto)", err) } response := httptest.NewRecorder() HowtoHandler(response, request) if response.Code != 200 { t.Log(response.Code) t.Fatal("Expected 200 as status code.") } request, err = http.NewRequest("GET", "/howto", nil) if err != nil { t.Log("Error creating new http request. (/howto)", err) } response = httptest.NewRecorder() cookie := http.Cookie{Name: "lang", Value: "de"} request.AddCookie(&cookie) HowtoHandler(response, request) if response.Code != 200 { t.Log(response.Code) t.Fatal("Expected 200 as status code. Language: de") } } func BenchmarkHowtoHandler(b *testing.B) { b.StopTimer() request, _ := http.NewRequest("GET", "/howto", nil) response := httptest.NewRecorder() b.StartTimer() for i := 0; i < b.N; i++ { HowtoHandler(response, request) } } func TestUserHandler(t *testing.T) { request, err := http.NewRequest("GET", "/user", nil) if err != nil { t.Log("Error creating new http request. (/user)", err) } response := httptest.NewRecorder() UserHandler(response, request) if response.Code != 200 { t.Log(response.Code) t.Fatal("Expected 200 as status code.") } request, err = http.NewRequest("GET", "/user", nil) if err != nil { t.Log("Error creating new http request. (/user)", err) } response = httptest.NewRecorder() cookie := http.Cookie{Name: "lang", Value: "de"} request.AddCookie(&cookie) UserHandler(response, request) if response.Code != 200 { t.Log(response.Code) t.Fatal("Expected 200 as status code. Language: de") } } func BenchmarkUserHandler(b *testing.B) { b.StopTimer() request, _ := http.NewRequest("GET", "/user", nil) response := httptest.NewRecorder() b.StartTimer() for i := 0; i < b.N; i++ { UserHandler(response, request) } } func TestPasswordHandler(t *testing.T) { request, err := http.NewRequest("GET", "/password", nil) if err != nil { t.Log("Error creating new http request. (/password)", err) } response := httptest.NewRecorder() PasswordHandler(response, request) if response.Code != 200 { t.Log(response.Code) t.Fatal("Expected 200 as status code.") } request, err = http.NewRequest("GET", "/password", nil) if err != nil { t.Log("Error creating new http request. (/password)", err) } response = httptest.NewRecorder() cookie := http.Cookie{Name: "lang", Value: "de"} request.AddCookie(&cookie) PasswordHandler(response, request) if response.Code != 200 { t.Log(response.Code) t.Fatal("Expected 200 as status code. Language: de") } } func BenchmarkPasswordHandler(b *testing.B) { b.StopTimer() request, _ := http.NewRequest("GET", "/password", nil) response := httptest.NewRecorder() b.StartTimer() for i := 0; i < b.N; i++ { PasswordHandler(response, request) } } func TestChangePasswordHandler(t *testing.T) { // Test with the correct form form := url.Values{} if err := CreateNewEntry("test@localhost", Md5Hash("Password")); err != nil { t.Fatal(err) } form.Set("Email", "test@localhost") form.Set("Password", "Password") form.Set("ConfirmPassword", "password") form.Set("NewPassword", "password") request, err := http.NewRequest("POST", "/changePassword", strings.NewReader(form.Encode())) if err != nil { t.Log("Error creating new http request. (/changePassword)", err) } request.Header.Add("Content-Type", "application/x-www-form-urlencoded") response := httptest.NewRecorder() ChangePasswordHandler(response, request) if response.Code != 302 { t.Log(response.Code) t.Fatal("Expected 302 as status code.") } flash := Flash{} session, err := store.Get(request, "_SID") if err != nil { t.Log(err) } flash.Error = session.Flashes("error") flash.Success = session.Flashes("success") if len(flash.Error) > 0 { for _, v := range flash.Error { t.Fatal(v) } t.Fatal("Got error message.") } if len(flash.Success) == 0 { t.Fatal("Expected success message.") } t.Log("Success message:", flash.Success[0]) // Test with incorrect form to check validation // Check Password and ConfirmPassword validation form = url.Values{} if err = CreateNewEntry("test1@localhost", Md5Hash("Password")); err != nil { t.Fatal(err) } form.Set("Email", "test1@localhost") form.Set("Password", "Password") form.Set("ConfirmPassword", "Password") form.Set("NewPassword", "password") request, err = http.NewRequest("POST", "/changePassword", strings.NewReader(form.Encode())) if err != nil { t.Log("Error creating new http request. (/changePassword)", err) } request.Header.Add("Content-Type", "application/x-www-form-urlencoded") response = httptest.NewRecorder() ChangePasswordHandler(response, request) if response.Code != 302 { t.Log(response.Code) t.Fatal("Expected 302 as status code.") } flash = Flash{} session, err = store.Get(request, "_SID") if err != nil { t.Log(err) } flash.Error = session.Flashes("error") flash.Success = session.Flashes("success") if len(flash.Success) > 0 { t.Fatal("Got success message, expected failure.") } for _, v := range flash.Error { t.Log(v) } // Test with incorrect form to check validation // Check password validation form = url.Values{} if err = CreateNewEntry("test2@localhost", Md5Hash("Password")); err != nil { t.Fatal(err) } form.Set("Email", "test2@localhost") form.Set("Password", "password") form.Set("ConfirmPassword", "Password") form.Set("NewPassword", "Password") request, err = http.NewRequest("POST", "/changePassword", strings.NewReader(form.Encode())) if err != nil { t.Log("Error creating new http request. (/changePassword)", err) } request.Header.Add("Content-Type", "application/x-www-form-urlencoded") response = httptest.NewRecorder() ChangePasswordHandler(response, request) if response.Code != 302 { t.Log(response.Code) t.Fatal("Expected 302 as status code.") } flash = Flash{} session, err = store.Get(request, "_SID") if err != nil { t.Log(err) } flash.Error = session.Flashes("error") flash.Success = session.Flashes("success") if len(flash.Success) > 0 { t.Fatal("Got success message, expected failure.") } for _, v := range flash.Error { t.Log(v) } // Test with incorrect form to check validation // Check non-existent e-mail form = url.Values{} form.Set("Email", "nonexistent@localhost") form.Set("Password", "password") form.Set("ConfirmPassword", "Password") form.Set("NewPassword", "Password") request, err = http.NewRequest("POST", "/changePassword", strings.NewReader(form.Encode())) if err != nil { t.Log("Error creating new http request. (/changePassword)", err) } request.Header.Add("Content-Type", "application/x-www-form-urlencoded") response = httptest.NewRecorder() ChangePasswordHandler(response, request) if response.Code != 302 { t.Log(response.Code) t.Fatal("Expected 302 as status code.") } flash = Flash{} session, err = store.Get(request, "_SID") if err != nil { t.Log(err) } flash.Error = session.Flashes("error") flash.Success = session.Flashes("success") if len(flash.Success) > 0 { t.Fatal("Got success message, expected failure.") } for _, v := range flash.Error { t.Log(v) } } func TestChangeLocaleHandler(t *testing.T) { // Test change locale to German request, err := http.NewRequest("GET", "/locale?to=de", nil) if err != nil { t.Log("Error creating new http request. (/locale?to=de)", err) } response := httptest.NewRecorder() ChangeLocaleHandler(response, request) if response.Code != 302 { t.Log(response.Code) t.Fatal("Expected 302 as status code.") } if response.HeaderMap["Set-Cookie"][0] != "lang=de" { t.Fatal("Expected Cookie 'lang=de") } // Test change locale to English request, err = http.NewRequest("GET", "/locale?to=en", nil) if err != nil { t.Log("Error creating new http request. (/locale?to=en)", err) } response = httptest.NewRecorder() ChangeLocaleHandler(response, request) if response.Code != 302 { t.Log(response.Code) t.Fatal("Expected 302 as status code.") } if response.HeaderMap["Set-Cookie"][0] != "lang=en" { t.Fatal("Expected Cookie 'lang=en'", response.HeaderMap["Set-Cookie"][0]) } // Test change locale to Garbage request, err = http.NewRequest("GET", "/locale?to=foobar", nil) if err != nil { t.Fatal("Error creating new http request. (/locale?to=foobar)", err) } response = httptest.NewRecorder() ChangeLocaleHandler(response, request) if response.Code != 302 { t.Log(response.Code) t.Fatal("Expected 302 as status code.") } if response.HeaderMap["Set-Cookie"][0] != "lang=foobar" { t.Fatal("Expected Cookie 'lang=foobar'", response.HeaderMap["Set-Cookie"][0]) } }