-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_test.go
114 lines (109 loc) · 4.28 KB
/
middleware_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
107
108
109
110
111
112
113
114
package htmxtools
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestParseRequest(t *testing.T) {
t.Parallel()
type testcase struct {
httpRequest *http.Request
htmxRequest *HTMXRequest
}
validRequest, _ := http.NewRequest(http.MethodGet, "localhost", nil)
validRequest.Header.Set(HXRequestHeader.String(), "true")
fullRequest := validRequest.Clone(context.TODO())
fullRequest.Header.Add(BoostedRequest.String(), "true")
fullRequest.Header.Add(CurrentURLRequest.String(), "localhost")
fullRequest.Header.Add(HistoryRestoreRequest.String(), "true")
fullRequest.Header.Add(PromptRequest.String(), "did you do it?")
fullRequest.Header.Add(TriggerRequest.String(), "add-thing")
fullRequest.Header.Add(TargetRequest.String(), "target-div")
fullRequest.Header.Add(TriggerNameRequest.String(), "thing-id")
testcases := map[string]testcase{
"nil": {httpRequest: &http.Request{}},
"validrequest": {httpRequest: validRequest, htmxRequest: &HTMXRequest{}},
"fullrequest": {httpRequest: fullRequest, htmxRequest: &HTMXRequest{
Boosted: true,
CurrentURL: "localhost",
HistoryRestore: true,
Prompt: "did you do it?",
Target: "target-div",
Trigger: "add-thing",
TriggerName: "thing-id",
}},
}
for n, tc := range testcases {
t.Run(n, func(t *testing.T) {
res := ParseRequest(tc.httpRequest)
if tc.htmxRequest == nil {
require.Nil(t, res)
} else {
require.NotNil(t, res)
require.Equal(t, tc.htmxRequest.Boosted, res.Boosted, "boosted should match")
require.Equal(t, tc.htmxRequest.CurrentURL, res.CurrentURL, "current url should match")
require.Equal(t, tc.htmxRequest.HistoryRestore, res.HistoryRestore, "history restore should match")
require.Equal(t, tc.htmxRequest.Prompt, res.Prompt, "prompt should match")
require.Equal(t, tc.htmxRequest.Target, res.Target, "target should match")
require.Equal(t, tc.htmxRequest.Trigger, res.Trigger, "trigger should match")
require.Equal(t, tc.htmxRequest.TriggerName, res.TriggerName, "trigger name should match")
}
})
}
}
func TestMiddleware(t *testing.T) {
t.Parallel()
htmxReq := &HTMXRequest{
Boosted: true,
CurrentURL: "localhost",
HistoryRestore: true,
Prompt: "did you do it?",
Target: "target-div",
Trigger: "add-thing",
TriggerName: "thing-id",
}
var extractedRequest *HTMXRequest
// need a func to wrap with the middleware and capture the context details
testHandlerFunc := func(w http.ResponseWriter, r *http.Request) {
extractedRequest = RequestFromContext(r.Context())
}
server := httptest.NewServer(WrapFunc(testHandlerFunc))
req, err := http.NewRequest(http.MethodGet, server.URL, nil)
require.NoError(t, err, "new request should not error")
require.NotNil(t, req, "http request should not be nil")
// do the request without the headers first
nres, nerr := http.DefaultClient.Do(req)
require.NoError(t, nerr, "request should not error")
require.NotNil(t, nres, "result should not be nil")
require.Nil(t, extractedRequest, "extracted result should be nil")
// add the htmx header
req.Header.Set(HXRequestHeader.String(), "true")
req.Header.Add(BoostedRequest.String(), "true")
req.Header.Add(CurrentURLRequest.String(), "localhost")
req.Header.Add(HistoryRestoreRequest.String(), "true")
req.Header.Add(PromptRequest.String(), "did you do it?")
req.Header.Add(TriggerRequest.String(), "add-thing")
req.Header.Add(TargetRequest.String(), "target-div")
req.Header.Add(TriggerNameRequest.String(), "thing-id")
res, err := http.DefaultClient.Do(req)
require.NoError(t, err, "making request should not error")
require.NotNil(t, res, "http request result should not be nil")
require.NotNil(t, extractedRequest, "request from context should not be nil")
require.Equal(t, htmxReq.Boosted, extractedRequest.Boosted)
}
// addHeaders adds hmtx headers to the provided http request for testing
func (hr *HTMXResponse) addHeaders(r *http.Request, headers ...map[HTMXRequestHeader]string) error { // nolint: unused
for _, h := range headers {
for k, v := range h {
if strings.TrimSpace(r.Header.Get(k.String())) != "" {
return fmt.Errorf("header already set: %s: %s", k, v)
}
r.Header.Set(k.String(), v)
}
}
return nil
}