-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrequest_builder_test.go
More file actions
48 lines (38 loc) · 1.06 KB
/
request_builder_test.go
File metadata and controls
48 lines (38 loc) · 1.06 KB
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
package rclient
import (
"errors"
"io/ioutil"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBuildJSONRequest(t *testing.T) {
req, err := BuildJSONRequest("GET", "www.domain.com/path", "body", Header("name", "val"))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "GET", req.Method)
assert.Equal(t, "www.domain.com/path", req.URL.String())
assert.Equal(t, "application/json", req.Header.Get("content-type"))
assert.Equal(t, "val", req.Header.Get("name"))
body, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "\"body\"\n", string(body))
}
func TestBuildJSONRequestNoBody(t *testing.T) {
req, err := BuildJSONRequest("GET", "www.domain.com/path", nil)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "", req.Header.Get("content-type"))
}
func TestBuildJSONRequest_optionError(t *testing.T) {
option := func(req *http.Request) error {
return errors.New("some error")
}
if _, err := BuildJSONRequest("GET", "www.domain.com/path", "body", option); err == nil {
t.Fatal("Error was nil!")
}
}