-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_test.go
87 lines (72 loc) · 2.13 KB
/
search_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
78
79
80
81
82
83
84
85
86
87
package goodreads
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestClient_Search(t *testing.T) {
var ctx = context.TODO()
t.Run("it decodes the search response into the given struct", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
content, _ := ioutil.ReadFile("fixtures/search_with_result.xml")
_, _ = fmt.Fprintln(w, string(content))
}))
defer ts.Close()
client := client{
APIKey: "123",
domain: ts.URL,
http: ts.Client(),
}
books, err := client.Search(ctx, "hairy pooter", 0)
assert.NoError(t, err)
assert.Equal(t, []Work{{
WorkID: 1111,
BookID: 35052265,
Title: "Harry Pooter and The Funny Guy",
ImageURL: "https://big.jpg",
SmallImageURL: "https://small.jpg",
Author: Author{
ID: 15388346,
Name: "John",
},
OriginalPublicationDate: OriginalPublicationDate{
Year: 2018,
Month: 8,
Day: 28,
},
}}, books)
})
t.Run("it decodes the search response into an empty slice if no result", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
content, _ := ioutil.ReadFile("fixtures/search_with_no_result.xml")
_, _ = fmt.Fprintln(w, string(content))
}))
defer ts.Close()
client := client{
APIKey: "123",
domain: ts.URL,
http: ts.Client(),
}
books, err := client.Search(ctx, "hairy pooter", 0)
assert.NoError(t, err)
assert.Equal(t, []Work{}, books)
})
t.Run("it returns an error if something went wrong", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "wtf", http.StatusInternalServerError)
}))
defer ts.Close()
client := client{
APIKey: "123",
domain: ts.URL,
http: ts.Client(),
}
books, err := client.Search(ctx, "hairy pooter", 0)
assert.EqualError(t, err, "'hairy pooter' search at page 0 failed: request failed for '//search/index': 500 Internal Server Error")
assert.Equal(t, []Work{}, books)
})
}