-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecoder_test.go
75 lines (66 loc) · 1.91 KB
/
decoder_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
package httpwrap
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/require"
)
type holder struct {
Body1 int `json:"body1"`
Query1 string `http:"query=query1"`
Query2 []int `http:"query=query2"`
Header1 *string `http:"header=header1"`
Cookie1 bool `http:"cookie=cookie1"`
Segment1 string `http:"segment=segment1"`
}
func TestDecoder(t *testing.T) {
t.Run("decode full", func(t *testing.T) {
body := strings.NewReader(`{"body1":42}`)
url := fmt.Sprintf("http://localhost/path?query1=query1val&query2=1&query2=2")
req := httptest.NewRequest("POST", url, body)
req.Header.Set("header1", "header1val")
req.AddCookie(&http.Cookie{Name: "cookie1", Value: "true"})
req.SetPathValue("segment1", "segment1val")
decoder := NewDecoder()
into := holder{}
err := decoder.Decode(req, &into)
require.NoError(t, err)
_headerVal := "header1val"
expected := holder{
Body1: 42,
Header1: &_headerVal,
Segment1: "segment1val",
Cookie1: true,
Query1: "query1val",
Query2: []int{1, 2},
}
require.Equal(t, expected, into)
})
}
func TestDecoderGorillaMux(t *testing.T) {
t.Run("decode full", func(t *testing.T) {
body := strings.NewReader(`{"body1":42}`)
url := fmt.Sprintf("http://localhost/path?query1=query1val&query2=1&query2=2")
req := httptest.NewRequest("POST", url, body)
req.Header.Set("header1", "header1val")
req.AddCookie(&http.Cookie{Name: "cookie1", Value: "true"})
req = mux.SetURLVars(req, map[string]string{"segment1": "segment1val"})
decoder := NewDecoder()
into := holder{}
err := decoder.Decode(req, &into)
require.NoError(t, err)
_headerVal := "header1val"
expected := holder{
Body1: 42,
Header1: &_headerVal,
Segment1: "segment1val",
Cookie1: true,
Query1: "query1val",
Query2: []int{1, 2},
}
require.Equal(t, expected, into)
})
}