Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/protocols/offlinehttp/read_response_test.go
2070 views
1
package offlinehttp
2
3
import (
4
"fmt"
5
"io"
6
"net/http"
7
"net/http/httptest"
8
"net/http/httputil"
9
"testing"
10
"time"
11
12
"github.com/julienschmidt/httprouter"
13
"github.com/stretchr/testify/require"
14
)
15
16
func TestReadResponseFromString(t *testing.T) {
17
expectedBody := `<!DOCTYPE html>
18
<html>
19
<head>
20
<title>Firing Range</title>
21
</head>
22
<body>
23
<h1>Version 0.48</h1>
24
<h1>What is the Firing Range?</h1>
25
<p>
26
</body>
27
</html>`
28
29
tests := []struct {
30
name string
31
data string
32
}{
33
{
34
name: "response",
35
data: `HTTP/1.1 200 OK
36
Age: 0
37
Cache-Control: public, max-age=600
38
Content-Type: text/html
39
Server: Google Frontend
40
41
<!DOCTYPE html>
42
<html>
43
<head>
44
<title>Firing Range</title>
45
</head>
46
<body>
47
<h1>Version 0.48</h1>
48
<h1>What is the Firing Range?</h1>
49
<p>
50
</body>
51
</html>`,
52
},
53
{
54
name: "response-http2-without-minor-version",
55
data: `HTTP/2 200 OK
56
Age: 0
57
Cache-Control: public, max-age=600
58
Content-Type: text/html
59
Server: Google Frontend
60
61
<!DOCTYPE html>
62
<html>
63
<head>
64
<title>Firing Range</title>
65
</head>
66
<body>
67
<h1>Version 0.48</h1>
68
<h1>What is the Firing Range?</h1>
69
<p>
70
</body>
71
</html>`,
72
},
73
{
74
name: "response-http2-with-minor-version",
75
data: `HTTP/2.0 200 OK
76
Age: 0
77
Cache-Control: public, max-age=600
78
Content-Type: text/html
79
Server: Google Frontend
80
81
<!DOCTYPE html>
82
<html>
83
<head>
84
<title>Firing Range</title>
85
</head>
86
<body>
87
<h1>Version 0.48</h1>
88
<h1>What is the Firing Range?</h1>
89
<p>
90
</body>
91
</html>`,
92
},
93
{
94
name: "request-response",
95
data: `GET http://public-firing-range.appspot.com/ HTTP/1.1
96
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
97
Accept-Encoding: gzip, deflate
98
Upgrade-Insecure-Requests: 1
99
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36
100
101
HTTP/1.1 200 OK
102
Age: 0
103
Cache-Control: public, max-age=600
104
Content-Type: text/html
105
Server: Google Frontend
106
107
<!DOCTYPE html>
108
<html>
109
<head>
110
<title>Firing Range</title>
111
</head>
112
<body>
113
<h1>Version 0.48</h1>
114
<h1>What is the Firing Range?</h1>
115
<p>
116
</body>
117
</html>`,
118
},
119
{
120
name: "request-response-without-minor-version",
121
data: `GET http://public-firing-range.appspot.com/ HTTP/1.1
122
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
123
Accept-Encoding: gzip, deflate
124
Upgrade-Insecure-Requests: 1
125
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36
126
127
HTTP/2 200 OK
128
Age: 0
129
Cache-Control: public, max-age=600
130
Content-Type: text/html
131
Server: Google Frontend
132
133
<!DOCTYPE html>
134
<html>
135
<head>
136
<title>Firing Range</title>
137
</head>
138
<body>
139
<h1>Version 0.48</h1>
140
<h1>What is the Firing Range?</h1>
141
<p>
142
</body>
143
</html>`,
144
},
145
}
146
147
for _, tt := range tests {
148
t.Run(tt.name, func(t *testing.T) {
149
resp, err := readResponseFromString(tt.data)
150
require.Nil(t, err, "could not read response from string")
151
152
respData, err := io.ReadAll(resp.Body)
153
require.Nil(t, err, "could not read response body")
154
require.Equal(t, expectedBody, string(respData), "could not get correct parsed body")
155
require.Equal(t, "Google Frontend", resp.Header.Get("Server"), "could not get correct headers")
156
})
157
}
158
159
t.Run("test-live-response-with-content-length", func(t *testing.T) {
160
var ts *httptest.Server
161
router := httprouter.New()
162
router.GET("/", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
163
w.Header().Add("Server", "Google Frontend")
164
_, _ = fmt.Fprintf(w, "%s", `<!DOCTYPE html>
165
<html>
166
<head>
167
<title>Firing Range</title>
168
</head>
169
<body>
170
<h1>Version 0.48</h1>
171
<h1>What is the Firing Range?</h1>
172
<p>
173
</body>
174
</html>`)
175
})
176
ts = httptest.NewServer(router)
177
defer ts.Close()
178
179
client := &http.Client{
180
Timeout: 3 * time.Second,
181
}
182
183
data, err := client.Get(ts.URL)
184
require.Nil(t, err, "could not dial url")
185
defer func() {
186
_ = data.Body.Close()
187
}()
188
189
b, err := httputil.DumpResponse(data, true)
190
require.Nil(t, err, "could not dump response")
191
192
respData, err := readResponseFromString(string(b))
193
require.Nil(t, err, "could not read response from string")
194
195
_, err = io.ReadAll(respData.Body)
196
require.Nil(t, err, "could not read response body")
197
198
require.Equal(t, "Google Frontend", respData.Header.Get("Server"), "could not get correct headers")
199
200
})
201
}
202
203