forked from exercism/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
57 lines (45 loc) · 1.25 KB
/
Copy pathclient_test.go
File metadata and controls
57 lines (45 loc) · 1.25 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
package api
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/exercism/cli/config"
"github.com/stretchr/testify/assert"
)
var (
mux *http.ServeMux
server *httptest.Server
client *Client
conf = &config.Config{APIKey: "apikey", API: "localhost", XAPI: "xlocalhost"}
)
func TestNewRequestSetsDefaultHeaders(t *testing.T) {
UserAgent = "Test"
client = NewClient(conf)
req, err := client.NewRequest("GET", client.APIHost, nil)
assert.NoError(t, err)
assert.Equal(t, UserAgent, req.Header.Get("User-Agent"))
assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
}
func TestDo(t *testing.T) {
UserAgent = "Exercism Test v1"
mux = http.NewServeMux()
server = httptest.NewServer(mux)
defer server.Close()
url := server.URL
conf = &config.Config{APIKey: "apikey", API: url, XAPI: url}
client = NewClient(conf)
type test struct {
T string
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
assert.Equal(t, UserAgent, r.Header.Get("User-Agent"))
fmt.Fprint(w, `{"T":"world"}`)
})
req, _ := client.NewRequest("GET", client.APIHost+"/", nil)
var body test
_, err := client.Do(req, &body)
assert.NoError(t, err)
assert.Equal(t, test{T: "world"}, body)
}