forked from peterssonjesper/booli-api-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresidence_test.go
66 lines (52 loc) · 1.44 KB
/
residence_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
package client
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestReturnsResidencesWhenResponseIsValidJson(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `{
"residences": [
{ "id" : 1234 }
]
}`)
}))
type envelope struct {
Residences []map[string]int
}
client := New(testServer.URL, "my-caller-id", "my-api-key")
body, err := client.Residences()
var e envelope
json.Unmarshal(body, &e)
if len(e.Residences) != 1 {
t.Error("Expected one sold property, got %#v", len(e.Residences))
}
if e.Residences[0]["id"] != 1234 {
t.Error("Expected booli ID to be 1234, was %#v", e.Residences[0]["id"])
}
if err != nil {
t.Error("Expected error to be nil, was %#v", err)
}
}
func TestJSONEncodesEstimatePayload(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
fmt.Fprintf(w, fmt.Sprintf("%s", body))
}))
client := New(testServer.URL, "my-caller-id", "my-api-key")
body, err := client.SubscribeToEstimation(map[string]interface{}{
"hello": "world",
"lat": 58.1,
})
if err != nil {
t.Error("Expected error to be nil, was %#v", err)
}
expected := `{"hello":"world","lat":58.1}`
if string(body) != expected {
t.Errorf("Expected body to be encoded to %s, but was %s", expected, body)
}
}