-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnshift_test.go
106 lines (88 loc) · 2.83 KB
/
Unshift_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package gorest_test
import (
"fmt"
"io"
"math/rand"
"net/http"
"strings"
"testing"
"github.com/adamluzsi/frameless/fixtures"
"github.com/adamluzsi/testcase"
"github.com/stretchr/testify/require"
"github.com/adamluzsi/gorest"
)
func TestUnshiftPathParamFromRequest(t *testing.T) {
s := testcase.NewSpec(t)
var subject = func(t *testcase.T) (*http.Request, string) {
return gorest.UnshiftPathParamFromRequest(t.I(`request`).(*http.Request))
}
s.Let(`request`, func(t *testcase.T) interface{} {
r, err := http.NewRequest(
t.I(`method`).(string),
t.I(`path`).(string),
t.I(`body`).(io.Reader),
)
require.Nil(t, err)
return r
})
s.Let(`method`, func(t *testcase.T) interface{} {
return fixtures.Random.ElementFromSlice([]string{
http.MethodGet,
http.MethodPost,
http.MethodPut,
http.MethodDelete,
http.MethodOptions,
http.MethodPatch,
http.MethodConnect,
http.MethodHead,
http.MethodTrace,
}).(string)
})
s.Let(`payload`, func(t *testcase.T) interface{} { return fixtures.Random.String() })
s.Let(`body`, func(t *testcase.T) interface{} { return strings.NewReader(t.I(`payload`).(string)) })
s.When(`request path has value but without slash prefix`, func(s *testcase.Spec) {
const value = `value`
s.Let(`path`, func(t *testcase.T) interface{} { return value })
s.Then(`it unshift the parameter`, func(t *testcase.T) {
r, param := subject(t)
require.Equal(t, value, param)
require.Equal(t, `/`, r.URL.Path)
})
})
s.When(`request path has value but with slash prefix`, func(s *testcase.Spec) {
const value = `value`
s.Let(`path`, func(t *testcase.T) interface{} { return fmt.Sprintf(`/%s`, value) })
s.Then(`it will unshift the parameter`, func(t *testcase.T) {
r, param := subject(t)
require.Equal(t, value, param)
require.Equal(t, `/`, r.URL.Path)
})
s.And(`not just one but multiple slashes`, func(s *testcase.Spec) {
s.Before(func(t *testcase.T) {
t.Let(`path`, strings.Repeat(`/`, rand.Intn(40)+2)+t.I(`path`).(string))
})
s.Then(`it will unshift the parameter`, func(t *testcase.T) {
r, param := subject(t)
require.Equal(t, value, param)
require.Equal(t, `/`, r.URL.Path)
})
})
})
s.When(`request path has multiple parts`, func(s *testcase.Spec) {
const value = `not so random value`
s.Let(`path`, func(t *testcase.T) interface{} { return fmt.Sprintf(`/%s/etc`, value) })
s.Then(`it will unshift the parameter`, func(t *testcase.T) {
r, param := subject(t)
require.Equal(t, value, param)
require.Equal(t, `/etc`, r.URL.Path)
})
})
s.When(`request path is empty`, func(s *testcase.Spec) {
s.Let(`path`, func(t *testcase.T) interface{} { return `` })
s.Then(`it will unshift the parameter`, func(t *testcase.T) {
r, param := subject(t)
require.Equal(t, ``, param)
require.Equal(t, `/`, r.URL.Path)
})
})
}