-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphant_test.go
More file actions
81 lines (63 loc) · 1.45 KB
/
phant_test.go
File metadata and controls
81 lines (63 loc) · 1.45 KB
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
78
79
80
81
package phant
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
var (
mux *http.ServeMux
server *httptest.Server
client *Client
serverURL string
)
func setup() {
mux = http.NewServeMux()
server = httptest.NewServer(mux)
serverURLParsed, _ := url.Parse(server.URL)
serverURL = serverURLParsed.String() + "/"
defaultEndpointPrefix = serverURL
client = Create("public", "private")
}
func teardown() {
server.Close()
}
func TestCreate(t *testing.T) {
c := Create("public", "private")
if c.publicKey != "public" {
t.Error("expected publicKey to be public")
}
if c.privateKey != "private" {
t.Error("expected privateKey to be private")
}
}
// some helpers
func handleSuccess() {
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{"success":true,"message":"ok"}`)
})
}
func handleError() {
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, `{"success":false,"message":"not ok"}`)
})
}
func TestParseErrorResponse(t *testing.T) {
setup()
defer teardown()
handleError()
req, err := createHTTPRequest("POST", serverURL, nil)
if err != nil {
t.Error("expected no error when creating request")
}
_, err = doAndParseRequest(req)
if err == nil {
t.Error("expected error in doAndParseRequest")
}
if err.Error() != "not ok" {
t.Error("expected 'not ok' in .Error()")
}
}