Skip to content

Commit 5faf5ca

Browse files
committed
router, action
1 parent f481e50 commit 5faf5ca

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

header_test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,23 @@ func TestNegotiateContentType(t *testing.T) {
114114

115115
req := createRawHTTPRequest(HeaderAccept, "application/json")
116116
req.URL, _ = url.Parse("http://localhost:8080/testpath.json")
117-
// req.Path = req.Raw.URL.Path
118117
contentType = NegotiateContentType(req)
119118
assert.Equal(t, "application/json", contentType.Mime)
120119
assert.Equal(t, ".json", contentType.Exts[0])
121120

122121
req = createRawHTTPRequest(HeaderAccept, "application/json")
123122
req.URL, _ = url.Parse("http://localhost:8080/testpath.html")
124-
// req.Path = req.Raw.URL.Path
125123
contentType = NegotiateContentType(req)
126124
assert.Equal(t, "text/html; charset=utf-8", contentType.Mime)
127125
assert.Equal(t, ".html", contentType.Exts[0])
128126

127+
req = createRawHTTPRequest(HeaderAccept, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
128+
contentType = NegotiateContentType(req)
129+
assert.Equal(t, "text/html", contentType.String())
130+
assert.Equal(t, "text/html", contentType.Mime)
131+
assert.Equal(t, "", contentType.Version())
132+
133+
// ParseAccept
129134
req = createRawHTTPRequest(HeaderAccept, "application/json; version=2")
130135
spec := ParseAccept(req, HeaderAccept).MostQualified()
131136
assert.Equal(t, "2", spec.GetParam("version", "1"))
@@ -137,6 +142,7 @@ func TestNegotiateContentType(t *testing.T) {
137142
req = createRawHTTPRequest(HeaderAccept, "application/json; version")
138143
spec = ParseAccept(req, HeaderAccept).MostQualified()
139144
assert.Equal(t, "", spec.GetParam("version", "1"))
145+
140146
}
141147

142148
func TestNegotiateEncoding(t *testing.T) {

request.go

+26-20
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ type Request struct {
4343
// and PATCH body parameters take precedence over URL query string values.
4444
Params url.Values
4545

46-
// PathParams contains values of request path parameters. e.g. /hello/{name}.
47-
PathParams url.Values
48-
4946
// Referer value of the HTTP 'Referrer' (or 'Referer') header.
5047
Referer string
5148

@@ -60,27 +57,36 @@ type Request struct {
6057
Raw *http.Request
6158
}
6259

63-
// ParseRequest method creates aah framework `ahttp.Request` instance from go
64-
// HTTP request.
65-
func ParseRequest(r *http.Request) *Request {
66-
req := &Request{
67-
Host: r.Host,
68-
Method: r.Method,
69-
Path: r.URL.Path,
70-
Header: r.Header,
71-
ContentType: ParseContentType(r),
72-
AcceptContentType: NegotiateContentType(r),
73-
Params: url.Values{},
74-
PathParams: url.Values{},
75-
Referer: getReferer(r.Header),
76-
ClientIP: ClientIP(r),
77-
Locale: NegotiateLocale(r),
78-
Raw: r,
79-
}
60+
// ParseRequest method populates the given aah framework `ahttp.Request`
61+
// instance from Go HTTP request.
62+
func ParseRequest(r *http.Request, req *Request) *Request {
63+
req.Host = r.Host
64+
req.Method = r.Method
65+
req.Path = r.URL.Path
66+
req.Header = r.Header
67+
req.ContentType = ParseContentType(r)
68+
req.AcceptContentType = NegotiateContentType(r)
69+
req.Params = url.Values{}
70+
req.Referer = getReferer(r.Header)
71+
req.ClientIP = ClientIP(r)
72+
req.Locale = NegotiateLocale(r)
73+
req.Raw = r
8074

8175
return req
8276
}
8377

78+
// Reset method resets request instance for reuse.
79+
func (r *Request) Reset() {
80+
r.Header = nil
81+
r.ContentType = nil
82+
r.AcceptContentType = nil
83+
r.Params = nil
84+
r.Referer = ""
85+
r.ClientIP = ""
86+
r.Locale = nil
87+
r.Raw = nil
88+
}
89+
8490
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
8591
// Unexported methods
8692
//___________________________________

request_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestParseRequest(t *testing.T) {
5555
req.Header.Add(HeaderAcceptLanguage, "en-gb;leve=1;q=0.8, da, en;level=2;q=0.7, en-us;q=gg")
5656
req.URL, _ = url.Parse("http://localhost:8000/welcome1.html?_ref=true")
5757

58-
aahReq := ParseRequest(req)
58+
aahReq := ParseRequest(req, &Request{})
5959

6060
assert.Equal(t, "127.0.0.1:8080", aahReq.Host)
6161
assert.Equal(t, "GET", aahReq.Method)

0 commit comments

Comments
 (0)