-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_test.go
44 lines (40 loc) · 1.13 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
// Copyright (c) 2017 LunaNode Hosting Inc. All right reserved.
// Use of this source code is governed by the MIT License. See LICENSE file.
package namesilo
import (
"net/http"
"net/http/httptest"
"testing"
)
func GetTestClient(t *testing.T, path string, response string) *Client {
var server *httptest.Server
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != path {
t.Errorf("expected client to request path %s, but got path %s", path, r.URL.Path)
}
w.Write([]byte(response))
go server.Close()
}))
return &Client{
HTTPClient: &http.Client{},
apiBaseURL: server.URL + "/",
}
}
func TestDecodeResponse(t *testing.T) {
var testResponse struct {
Response string `xml:"response"`
}
rawResponse := `<namesilo>
<reply>
<code>300</code>
<detail>success</detail>
<response>test</response>
</reply>
</namesilo>`
if err := decodeResponse([]byte(rawResponse), &testResponse); err != nil {
t.Fatalf("unexpected decode response error: %v", err)
}
if testResponse.Response != "test" {
t.Fatalf("response is '%s' but expected 'test'", testResponse.Response)
}
}