forked from ant0ine/go-json-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_test.go
72 lines (58 loc) · 1.64 KB
/
request_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
package rest
import (
"net/http"
"net/url"
"testing"
)
func defaultRequest(uri string, method string, t *testing.T) Request {
urlObj, err := url.Parse(uri)
if err != nil {
t.Fatal()
}
origReq := http.Request{
Method: method,
URL: urlObj,
Host: "localhost",
}
req := Request{&origReq, nil}
return req
}
func TestRequestUriBase(t *testing.T) {
req := defaultRequest("http://localhost", "GET", t)
uriBase := req.UriBase()
uriString := uriBase.String()
expected := "http://localhost"
if uriString != expected {
t.Error(expected + " was the expected URI base, but instead got " + uriString)
}
}
func TestRequestUriScheme(t *testing.T) {
req := defaultRequest("https://localhost", "GET", t)
uriBase := req.UriBase()
expected := "https"
if uriBase.Scheme != expected {
t.Error(expected + " was the expected scheme, but instead got " + uriBase.Scheme)
}
}
func TestRequestUriFor(t *testing.T) {
req := defaultRequest("http://locahost", "GET", t)
path := "/foo/bar"
uri := req.UriFor(path)
if uri.Path != path {
t.Error(path + " was expected to be the path, but got " + uri.Path)
}
expected := "http://localhost/foo/bar"
if uri.String() != expected {
t.Error(expected + " was expected, but the returned URI was " + uri.String())
}
}
func TestRequestUriForParams(t *testing.T) {
req := defaultRequest("http://localhost", "GET", t)
params := make(map[string][]string)
params["id"] = []string{"foo", "bar"}
uri := req.UriForWithParams("/foo/bar", params)
expected := "http://localhost/foo/bar?id=foo&id=bar"
if uri.String() != expected {
t.Error(expected + " was expected, but the returned URI was " + uri.String())
}
}