-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
77 lines (64 loc) · 1.33 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"golang.org/x/net/publicsuffix"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"testing"
)
var jar *cookiejar.Jar
var client *http.Client
func init() {
var err error
jar, err = cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
log.Fatal(err)
}
client = &http.Client{
Jar: jar,
}
}
func TestLogin(t *testing.T) {
u, err := url.Parse("http://localhost:1337/api/auth/login")
if err != nil {
t.Fatal(err)
}
resp, err := client.Get(u.String())
if err != nil {
t.Fatal(err)
}
got := resp.StatusCode
want := http.StatusOK
if got != want {
t.Fatalf("resp.StatusCode = %d; want %d", got, want)
}
if jar.Cookies(u)[0].Name != "sid" {
t.Fatalf("Cookie.Name = %s; want %s", jar.Cookies(u)[0].Name, "sid")
}
}
func TestSecret(t *testing.T) {
u, err := url.Parse("http://localhost:1337/secret")
if err != nil {
t.Fatal(err)
}
resp, err := client.Get(u.String())
if err != nil {
t.Fatal(err)
}
got := resp.StatusCode
want := http.StatusOK
if got != want {
t.Fatalf("resp.StatusCode = %d; want %d", got, want)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
gotBody := string(body)
wantBody := "My little Secret"
if gotBody != wantBody {
t.Fatalf("string(body) = %s; want %s", gotBody, wantBody)
}
}