-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexchange_test.go
More file actions
71 lines (63 loc) · 1.58 KB
/
exchange_test.go
File metadata and controls
71 lines (63 loc) · 1.58 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
package entrust
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"testing"
)
func TestExchangeMethodGet(t *testing.T) {
c := &Client{
client: &http.Client{},
}
requestPath := "/path"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != requestPath {
t.Errorf("got path %q expected %s", r.URL.Path, requestPath)
}
if r.Method != http.MethodGet {
t.Errorf("got method %q expected %s", r.Method, http.MethodGet)
}
if r.ContentLength > 0 {
t.Errorf("got content lenght %d expected 0", r.ContentLength)
}
fmt.Fprintln(w, "Done")
}))
defer ts.Close()
APIServer = ts.URL
_, _ = c.exchange(requestPath, http.MethodGet, nil)
}
func TestExchangeMethodPost(t *testing.T) {
c := &Client{
client: &http.Client{},
}
payload := map[string]bool{"test": true}
jsonPayload, err := json.Marshal(payload)
if err != nil {
t.FailNow()
}
requestPath := "/path"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != requestPath {
t.Errorf("got path %q expected %s", r.URL.Path, requestPath)
}
if r.Method != http.MethodPost {
t.Errorf("got method %q expected %s", r.Method, http.MethodPost)
}
pl, err := io.ReadAll(r.Body)
r.Body.Close()
if err != nil {
log.Fatal(err)
}
if !bytes.Equal(jsonPayload, pl) {
t.Errorf("got payload %s expected %s", string(pl), string(jsonPayload))
}
fmt.Fprintln(w, "Done")
}))
defer ts.Close()
APIServer = ts.URL
_, _ = c.exchange(requestPath, http.MethodPost, payload)
}