forked from peterhellberg/hn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
63 lines (50 loc) · 1.49 KB
/
client_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
package hn
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestNewClient(t *testing.T) {
c := NewClient()
if got, want := c.BaseURL.String(), "https://hacker-news.firebaseio.com/v0/"; got != want {
t.Fatalf(`c.BaseURL.String() = %q, want %q`, got, want)
}
if got, want := c.UserAgent, "hn.go/0.0.2"; got != want {
t.Fatalf(`c.UserAgent = %q, want %q`, got, want)
}
}
func TestNewRequest(t *testing.T) {
r, err := NewClient(nil).NewRequest(context.Background(), fmt.Sprintf("foo?bar=%v", 123))
if err != nil {
t.Fatalf(`err != nil, got %v`, err)
}
if got, want := r.URL.String(), "https://hacker-news.firebaseio.com/v0/foo?bar=123"; got != want {
t.Fatalf(`r.URL.String() = %q, want %q`, got, want)
}
}
func TestNewRequest_invalidPath(t *testing.T) {
if _, err := NewClient(nil).NewRequest(context.Background(), "%"); err == nil {
t.Fatalf("expected to get error for invalid path")
}
}
func testServerAndClient(body []byte) (*httptest.Server, *Client) {
ts := testServer(body)
c := DefaultClient
c.BaseURL, _ = url.Parse(ts.URL)
return ts, c
}
func testServerAndClientByFixture(fn string) (*httptest.Server, *Client) {
body, _ := ioutil.ReadFile("_fixtures/" + fn + ".json")
return testServerAndClient(body)
}
func testServer(body []byte) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write(body)
}))
}