-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathhttp_test.go
More file actions
113 lines (96 loc) · 3.91 KB
/
http_test.go
File metadata and controls
113 lines (96 loc) · 3.91 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
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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package x
import (
"context"
"crypto/tls"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"github.com/ory/herodot"
"github.com/ory/x/assertx"
"github.com/ory/x/logrusx"
"github.com/stretchr/testify/assert"
"github.com/ory/x/urlx"
)
func TestWithBaseURL(t *testing.T) {
ctx := WithBaseURL(context.Background(), urlx.ParseOrPanic("https://www.ory.sh/"))
assert.EqualValues(t, "https://www.ory.sh/", BaseURLFromContext(ctx).String())
assert.Nil(t, BaseURLFromContext(context.Background()))
}
func TestRequestURL(t *testing.T) {
assert.EqualValues(t, RequestURL(&http.Request{
URL: urlx.ParseOrPanic("/foo"), Host: "foobar", TLS: &tls.ConnectionState{},
}).String(), "https://foobar/foo")
assert.EqualValues(t, RequestURL(&http.Request{
URL: urlx.ParseOrPanic("/foo"), Host: "foobar",
}).String(), "http://foobar/foo")
assert.EqualValues(t, RequestURL(&http.Request{
URL: urlx.ParseOrPanic("/foo"), Host: "foobar", Header: http.Header{"X-Forwarded-Host": []string{"notfoobar"}, "X-Forwarded-Proto": {"https"}},
}).String(), "https://notfoobar/foo")
assert.EqualValues(t, RequestURL(&http.Request{
URL: urlx.ParseOrPanic("/self-service/login/browser?flow=123"),
Host: "foobar",
Header: http.Header{
"X-Forwarded-Host": []string{"notfoobar"},
"X-Forwarded-Proto": []string{"https"},
"X-Forwarded-Prefix": []string{"/.ory/kratos"},
},
}).String(), "https://notfoobar/.ory/kratos/self-service/login/browser?flow=123")
assert.EqualValues(t, RequestURL(&http.Request{
URL: urlx.ParseOrPanic("/.ory/kratos/self-service/login/browser?flow=123"),
Host: "foobar",
Header: http.Header{
"X-Forwarded-Host": []string{"notfoobar"},
"X-Forwarded-Proto": []string{"https"},
"X-Forwarded-Prefix": []string{"/.ory/kratos"},
},
}).String(), "https://notfoobar/.ory/kratos/self-service/login/browser?flow=123")
}
func TestAcceptToRedirectOrJSON(t *testing.T) {
wr := herodot.NewJSONWriter(logrusx.New("", ""))
t.Run("case=browser", func(t *testing.T) {
r := httptest.NewRequest("GET", "/", nil)
r.Header.Set("Accept", "text/html")
t.Run("regular payload", func(t *testing.T) {
w := httptest.NewRecorder()
SendFlowCompletedAsRedirectOrJSON(w, r, wr, json.RawMessage(`{"foo":"bar"}`), "https://www.ory.sh/redir")
loc, err := w.Result().Location()
require.NoError(t, err)
assert.Equal(t, "https://www.ory.sh/redir", loc.String())
})
t.Run("error payload", func(t *testing.T) {
w := httptest.NewRecorder()
SendFlowCompletedAsRedirectOrJSON(w, r, wr, errors.New("foo"), "https://www.ory.sh/redir")
loc, err := w.Result().Location()
require.NoError(t, err)
assert.Equal(t, "https://www.ory.sh/redir", loc.String())
})
})
t.Run("case=json", func(t *testing.T) {
r := httptest.NewRequest("GET", "/", nil)
r.Header.Set("Accept", "application/json")
t.Run("regular payload", func(t *testing.T) {
msg := json.RawMessage(`{"foo":"bar"}`)
w := httptest.NewRecorder()
SendFlowCompletedAsRedirectOrJSON(w, r, wr, msg, "https://www.ory.sh/redir")
_, err := w.Result().Location()
require.ErrorIs(t, err, http.ErrNoLocation)
body := MustReadAll(w.Result().Body)
assertx.EqualAsJSON(t, msg, json.RawMessage(body))
})
t.Run("error payload", func(t *testing.T) {
ee := errors.WithStack(herodot.ErrBadRequest)
w := httptest.NewRecorder()
SendFlowCompletedAsRedirectOrJSON(w, r, wr, ee, "https://www.ory.sh/redir")
_, err := w.Result().Location()
require.ErrorIs(t, err, http.ErrNoLocation)
body := MustReadAll(w.Result().Body)
assertx.EqualAsJSON(t, map[string]interface{}{"error": map[string]interface{}{"code": 400, "message": "The request was malformed or contained invalid parameters", "status": "Bad Request"}}, json.RawMessage(body))
assert.EqualValues(t, http.StatusBadRequest, w.Result().StatusCode)
})
})
}