-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtesting.go
51 lines (44 loc) · 1.12 KB
/
testing.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
package gocardless
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func runServer(t *testing.T, fixtureFile string, method string) *httptest.Server {
f, _ := os.Open(fixtureFile)
defer f.Close()
responseBody, err := io.ReadAll(f)
if err != nil {
t.Fatal(err)
}
var response map[string]interface{}
json.Unmarshal(responseBody, &response)
body, ok := response[method].(map[string]interface{})
if !ok {
t.Fatal(fmt.Printf("error, response[%s] is nil or value stored is not of type map[string]interface", method))
}
bodyStr, ok := body["body"].(map[string]interface{})
if !ok {
t.Fatal("error, body[\"body\"] is nil or value stored is not of type map[string]interface")
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(bodyStr)
}))
return server
}
func getClient(t *testing.T, url string) (*Service, error) {
token := "dummy_token"
config, err := NewConfig(token, WithEndpoint(url))
if err != nil {
t.Fatal(err)
}
service, err := New(config)
if err != nil {
t.Fatal(err)
}
return service, nil
}