]> gitweb.factorcode.org Git - factor.git/blob - basis/http/client/client-tests.factor
core: Add words/unwords/unwords-as and use them.
[factor.git] / basis / http / client / client-tests.factor
1 USING: accessors http http.client http.client.private
2 io.streams.string kernel namespaces sequences tools.test urls ;
3 IN: http.client.tests
4
5 { "foo.txt" } [ "http://www.paulgraham.com/foo.txt" download-name ] unit-test
6 { "foo.txt" } [ "http://www.arc.com/foo.txt?xxx" download-name ] unit-test
7 { "foo.txt" } [ "http://www.arc.com/foo.txt/" download-name ] unit-test
8 { "www.arc.com" } [ "http://www.arc.com////" download-name ] unit-test
9
10 {
11     T{ request
12         { url T{ url { protocol "http" } { host "www.apple.com" } { port 80 } { path "/index.html" } } }
13         { proxy-url T{ url } }
14         { method "GET" }
15         { version "1.1" }
16         { cookies V{ } }
17         { header H{ { "Connection" "close" } { "User-Agent" "Factor http.client" } } }
18         { redirects 10 }
19     }
20 } [
21     "http://www.apple.com/index.html"
22     <get-request>
23 ] unit-test
24
25 {
26     T{ request
27         { url T{ url { protocol "https" } { host "www.amazon.com" } { port 443 } { path "/index.html" } } }
28         { proxy-url T{ url } }
29         { method "GET" }
30         { version "1.1" }
31         { cookies V{ } }
32         { header H{ { "Connection" "close" } { "User-Agent" "Factor http.client" } } }
33         { redirects 10 }
34     }
35 } [
36     "https://www.amazon.com/index.html"
37     <get-request>
38 ] unit-test
39
40 { "HEAD" } [ "http://google.com" <head-request> method>> ] unit-test
41 { "DELETE" } [ "http://arc.com" <delete-request> method>> ] unit-test
42 { "TRACE" } [ "http://concatenative.org" <trace-request> method>> ] unit-test
43 { "OPTIONS" } [ "http://factorcode.org" <options-request> method>> ] unit-test
44
45 ! Do not re-enable this for the test suite.
46 ! We should replace this with a similar test that does not
47 ! hit the velox.ch website.
48 ! { t } [
49     ! "https://alice.sni.velox.ch" http-get nip
50     ! [ "Great!" swap subseq? ]
51     ! [ "TLS SNI Test Site: alice.sni.velox.ch" swap subseq? ] bi and
52 ! ] unit-test
53
54 { t } [
55     {
56         "HTTP/1.1 200 Document follows"
57         "connection: close"
58         "content-type: text/html; charset=UTF-8"
59         "date: Wed, 12 Oct 2011 18:57:49 GMT"
60         "server: Factor http.server"
61     } [ unlines ] [ "\r\n" join ] bi
62     [ [ read-response ] with-string-reader ] same?
63 ] unit-test
64
65 { "www.google.com:8080" } [
66     URL" http://foo:bar@www.google.com:8080/foo?bar=baz#quux" authority-uri
67 ] unit-test
68
69 { "/index.html?bar=baz" } [
70     "http://user:pass@www.apple.com/index.html?bar=baz#foo"
71     <get-request>
72         f >>proxy-url
73     request-uri
74 ] unit-test
75
76 { "/index.html?bar=baz" } [
77     "https://user:pass@www.apple.com/index.html?bar=baz#foo"
78     <get-request>
79         f >>proxy-url
80     request-uri
81 ] unit-test
82
83 { "http://www.apple.com/index.html?bar=baz" } [
84     "http://user:pass@www.apple.com/index.html?bar=baz#foo"
85     <get-request>
86         "http://localhost:3128" >>proxy-url
87     request-uri
88 ] unit-test
89
90 { "www.apple.com:80" } [
91     "http://user:pass@www.apple.com/index.html?bar=baz#foo"
92     "CONNECT" <client-request>
93         f >>proxy-url
94     request-uri
95 ] unit-test
96
97 { "www.apple.com:443" } [
98     "https://www.apple.com/index.html"
99     "CONNECT" <client-request>
100         f >>proxy-url
101      request-uri
102 ] unit-test
103
104 { f } [
105     "" "no_proxy" [
106         "www.google.fr" <get-request> no-proxy?
107     ] with-variable
108 ] unit-test
109
110 { f } [
111     "," "no_proxy" [
112         "www.google.fr" <get-request> no-proxy?
113     ] with-variable
114 ] unit-test
115
116 { f } [
117     "foo,,bar" "no_proxy" [
118         "www.google.fr" <get-request> no-proxy?
119     ] with-variable
120 ] unit-test
121
122 { t } [
123     "foo,www.google.fr,bar" "no_proxy" [
124         "www.google.fr" <get-request> no-proxy?
125     ] with-variable
126 ] unit-test
127
128 ! TODO support 192.168.0.16/4 ?
129 CONSTANT: classic-proxy-settings H{
130     { "http.proxy" "http://proxy.private:3128" }
131     { "https.proxy" "http://proxysec.private:3128" }
132     { "no_proxy" "localhost,127.0.0.1,.allprivate,.a.subprivate,b.subprivate" }
133 }
134
135 { f } [
136     classic-proxy-settings [
137        "localhost" "GET" <client-request> ?default-proxy proxy-url>>
138     ] with-variables
139 ] unit-test
140
141 { f } [
142     classic-proxy-settings [
143        "127.0.0.1" "GET" <client-request> ?default-proxy proxy-url>>
144     ] with-variables
145 ] unit-test
146
147 { URL" http://proxy.private:3128" } [
148     classic-proxy-settings [
149        "27.0.0.1" "GET" <client-request> ?default-proxy proxy-url>>
150     ] with-variables
151 ] unit-test
152
153 { f } [
154     classic-proxy-settings [
155        "foo.allprivate" "GET" <client-request> ?default-proxy proxy-url>>
156     ] with-variables
157 ] unit-test
158
159 { f } [
160     classic-proxy-settings [
161        "bar.a.subprivate" "GET" <client-request> ?default-proxy proxy-url>>
162     ] with-variables
163 ] unit-test
164
165 { URL" http://proxy.private:3128" } [
166     classic-proxy-settings [
167        "a.subprivate" "GET" <client-request> ?default-proxy proxy-url>>
168     ] with-variables
169 ] unit-test
170
171 { f } [
172     classic-proxy-settings [
173        "bar.b.subprivate" "GET" <client-request> ?default-proxy proxy-url>>
174     ] with-variables
175 ] unit-test
176
177 { f } [
178     classic-proxy-settings [
179        "b.subprivate" "GET" <client-request> ?default-proxy proxy-url>>
180     ] with-variables
181 ] unit-test
182
183 { URL" http://proxy.private:3128" } [
184     classic-proxy-settings [
185        "bara.subprivate" "GET" <client-request> ?default-proxy proxy-url>>
186     ] with-variables
187 ] unit-test
188
189 { URL" http://proxy.private:3128" } [
190     classic-proxy-settings [
191        "google.com" "GET" <client-request> ?default-proxy proxy-url>>
192     ] with-variables
193 ] unit-test
194
195 { URL" //localhost:3128" } [
196     { { "http.proxy" "//localhost:3128" } } [
197        "google.com" "GET" <client-request> ?default-proxy proxy-url>>
198     ] with-variables
199 ] unit-test
200
201 { URL" //localhost:3128" } [
202     "google.com" "GET" <client-request>
203     URL" //localhost:3128" >>proxy-url ?default-proxy proxy-url>>
204 ] unit-test
205
206 { URL" //localhost:3128" } [
207     "google.com" "GET" <client-request>
208     "//localhost:3128" >>proxy-url ?default-proxy proxy-url>>
209 ] unit-test
210
211 { URL" http://proxysec.private:3128" } [
212     classic-proxy-settings [
213        "https://google.com" "GET" <client-request> ?default-proxy proxy-url>>
214     ] with-variables
215 ] unit-test
216
217 { URL" http://proxy.private:3128" } [
218     classic-proxy-settings [
219        "allprivate.google.com" "GET" <client-request> ?default-proxy proxy-url>>
220     ] with-variables
221 ] unit-test
222
223 [
224     <url> 3128 >>port "http.proxy" [
225        "http://www.google.com" "GET" <client-request> ?default-proxy
226     ] with-variable
227 ] [ invalid-proxy? ] must-fail-with
228
229 ! This url is misparsed bu request-url can fix it
230 { T{ url
231    { protocol "http" }
232    { host "www.google.com" }
233    { path "/" }
234    { port 80 }
235 } } [ "www.google.com" request-url ] unit-test
236
237 ! This one is not fixable, leave it as it is
238 { T{ url } } [ "" request-url ] unit-test