forked from plivo/plivo-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutils_test.go
86 lines (68 loc) · 2.41 KB
/
testutils_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
package plivo
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"unicode"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
var expectedStatusCode = 200
var expectedResponse = ""
var requestUrl *url.URL
var requestMethod string
var requestHeader http.Header
var testAuthId="AuthId"
var testAuthToken="AuthId"
var server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestUrl = r.URL
requestMethod = r.Method
requestHeader = r.Header
w.WriteHeader(expectedStatusCode)
w.Write([]byte(expectedResponse))
log.Infoln(expectedResponse)
}))
func expectResponse(fixturePath string, statusCode int) {
if fixturePath != "" {
// Some of the tests use uppercased names, so fix up them up.
fixturePathRunes := []rune(fixturePath)
fixturePathRunes[0] = unicode.ToLower(fixturePathRunes[0])
fullFixturePath := fmt.Sprintf("fixtures/%s", string(fixturePathRunes))
contents, err := ioutil.ReadFile(fullFixturePath)
if err != nil {
panic(err)
}
log.Infof("Loaded fixture from %s (%d)\n", fullFixturePath, len(contents))
expectedResponse = string(contents)
} else {
expectedResponse = ""
}
expectedStatusCode = statusCode
}
func assertRequest(t *testing.T, method, path string, params ...interface{}) {
path = fmt.Sprintf("/v1/Account/%s/%s/", client.AuthId, fmt.Sprintf(path, params...))
expectedUrl, _ := url.Parse(path)
if expectedUrl.Path != requestUrl.Path || method != requestMethod {
log.Printf("expectedUrl: %s, requestUrl %s\nexpectedMethod: %s, requestMethod: %s\n", expectedUrl, requestUrl, method, requestMethod)
t.FailNow()
}
assert.Contains(t, requestHeader.Get("User-Agent"), "plivo-go")
}
func assertPhloRequest(t *testing.T, method, path string, params ...interface{}) {
path = fmt.Sprintf("/v1/%s", fmt.Sprintf(path, params...))
expectedUrl, _ := url.Parse(path)
if expectedUrl.Path != requestUrl.Path || method != requestMethod {
log.Printf("expectedUrl: %s, requestUrl %s\nexpectedMethod: %s, requestMethod: %s\n", expectedUrl, requestUrl, method, requestMethod)
t.FailNow()
}
assert.Contains(t, requestHeader.Get("User-Agent"), "plivo-go")
}
func init() {
client.BaseUrl, _ = url.Parse(server.URL)
phloClient.BaseUrl,_ = url.Parse(server.URL)
}
var client, _ = NewClient(testAuthId, testAuthToken, &ClientOptions{})
var phloClient, _ = NewPhloClient(testAuthId,testAuthToken , &ClientOptions{})