82 lines
1.6 KiB
Go
82 lines
1.6 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"reflect"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
type StubUserStore struct {
|
||
|
userIpMap map[string]string
|
||
|
}
|
||
|
|
||
|
func (s *StubUserStore) GetUserName(name string) string {
|
||
|
return name
|
||
|
}
|
||
|
|
||
|
func (s *StubUserStore) ShowIP(name string) string {
|
||
|
return s.userIpMap[name]
|
||
|
}
|
||
|
|
||
|
func AssertNoError(t testing.TB, err error) {
|
||
|
t.Helper()
|
||
|
if err != nil {
|
||
|
t.Fatalf("didn't expect an error, but got one, %v", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func NewGetHomeRequest() *http.Request {
|
||
|
req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("/"), nil)
|
||
|
return req
|
||
|
}
|
||
|
|
||
|
func NewPutHomeRequest(userName string) *http.Request {
|
||
|
req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("/"+userName), nil)
|
||
|
return req
|
||
|
}
|
||
|
|
||
|
func userInUserList(ul UserList, user User) bool {
|
||
|
for _, u := range ul {
|
||
|
if reflect.DeepEqual(u, user) {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func AssertResponseBody(t testing.TB, got, want string) {
|
||
|
t.Helper()
|
||
|
if got != want {
|
||
|
t.Errorf("response body is wrong, got %q want %q", got, want)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func AssertStatus(t testing.TB, got, want int) {
|
||
|
t.Helper()
|
||
|
if got != want {
|
||
|
t.Errorf("did not get correct status, expected %d got %d", got, want)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func AssertUsers(t testing.TB, got, want []User) {
|
||
|
t.Helper()
|
||
|
if !reflect.DeepEqual(got, want) {
|
||
|
t.Errorf("got %v want %v", got, want)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func AssertUserExists(t testing.TB, got []User, want User) {
|
||
|
t.Helper()
|
||
|
if !userInUserList(got, want) {
|
||
|
t.Errorf("got %v want %v", got, want)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func AssertUserNotExists(t testing.TB, got []User, want User) {
|
||
|
t.Helper()
|
||
|
if userInUserList(got, want) {
|
||
|
t.Errorf("Expected %v to be deleted but it was not. List is %v", want, got)
|
||
|
}
|
||
|
}
|