@@ -24,7 +24,8 @@ import Data.Text.Encoding qualified as T
2424import Data.Text.IO qualified as TIO
2525import Network.HTTP.Client qualified as L
2626import Network.HTTP.Client.MultipartFormData qualified as LM
27−import Network.HTTP.Req
27+import Network.HTTP.Req hiding (req)
28+import Network.HTTP.Req qualified as Req
2829import Network.HTTP.Types qualified as Y
2930import Test.Hspec
3031import Test.QuickCheck
@@ -33,25 +34,25 @@ spec :: Spec
3334spec = do
3435 describe "exception throwing on non-2xx status codes" $
3536 it "throws indeed for non-2xx" $
36− req GET (httpbun /: "foo") NoReqBody ignoreResponse mempty
37+ req GET (httpbin /: "foo") NoReqBody ignoreResponse mempty
3738 `shouldThrow` selector404
3839
3940 describe "exception throwing on non-2xx status codes (Req monad)" $
4041 it "throws indeed for non-2xx" $
4142 asIO . runReq defaultHttpConfig $
4243 liftBaseWith $ \run ->
43− run (req GET (httpbun /: "foo") NoReqBody ignoreResponse mempty)
44+ run (req GET (httpbin /: "foo") NoReqBody ignoreResponse mempty)
4445 `shouldThrow` selector404
4546
4647 describe "response check via httpConfigCheckResponse" $
4748 context "if it's set to always throw" $
4849 it "throws indeed" $
49− blindlyThrowing (req GET httpbun NoReqBody ignoreResponse mempty)
50+ blindlyThrowing (req GET httpbin NoReqBody ignoreResponse mempty)
5051 `shouldThrow` anyException
5152
5253 describe "isStatusCodeException" $
5354 it "extracts non-2xx response" $
54− req GET (httpbun /: "foo") NoReqBody ignoreResponse mempty
55+ req GET (httpbin /: "foo") NoReqBody ignoreResponse mempty
5556 `shouldThrow` selector404ByStatusCodeException
5657
5758 describe "receiving user-agent header back" $
@@ -59,7 +60,7 @@ spec = do
5960 r <-
6061 req
6162 GET
62− (httpbun /: "user-agent")
63+ (httpbin /: "user-agent")
6364 NoReqBody
6465 jsonResponse
6566 (header "user-agent" "Req")
@@ -74,27 +75,29 @@ spec = do
7475 r <-
7576 req
7677 GET
77− (httpbun /: "headers")
78+ (httpbin /: "headers")
7879 NoReqBody
7980 jsonResponse
8081 (header "Foo" "bar" <> header "Baz" "quux")
81− stripFunnyHeaders' (responseBody r)
82+ stripFunnyHeaders (responseBody r)
8283 `shouldBe` object
83− [ "Accept-Encoding" .= ("gzip" :: Text),
84− "Foo" .= ("bar" :: Text),
85− "Baz" .= ("quux" :: Text)
84+ [ "headers"
85+ .= object
86+ [ "Accept-Encoding" .= ("gzip" :: Text),
87+ "Foo" .= ("bar" :: Text),
88+ "Baz" .= ("quux" :: Text)
89+ ]
8690 ]
8791 responseStatusCode r `shouldBe` 200
8892 responseStatusMessage r `shouldBe` "OK"
8993
9094 describe "receiving GET data back" $
9195 it "works" $ do
92− r <- req GET (httpbun /: "get") NoReqBody jsonResponse mempty
96+ r <- req GET (httpbin /: "get") NoReqBody jsonResponse mempty
9397 (stripFunnyHeaders . stripOrigin) (responseBody r)
9498 `shouldBe` object
9599 [ "args" .= emptyObject,
96− "url" .= ("https://httpbun.org/get" :: Text),
97− "method" .= ("GET" :: Text),
100+ "url" .= ("http://localhost:1234/get" :: Text),
98101 "headers"
99102 .= object
100103 [ "Accept-Encoding" .= ("gzip" :: Text)
@@ -108,14 +111,13 @@ spec = do
108111 it "works" $ do
109112 let text = "foo" :: Text
110113 reflected = reflectJSON text
111− r <- req POST (httpbun /: "post") (ReqBodyJson text) jsonResponse mempty
114+ r <- req POST (httpbin /: "post") (ReqBodyJson text) jsonResponse mempty
112115 (stripFunnyHeaders . stripOrigin) (responseBody r)
113116 `shouldBe` object
114117 [ "args" .= emptyObject,
115118 "json" .= text,
116119 "data" .= reflected,
117− "url" .= ("https://httpbun.org/post" :: Text),
118− "method" .= ("POST" :: Text),
120+ "url" .= ("http://localhost:1234/post" :: Text),
119121 "headers"
120122 .= object
121123 [ "Content-Type" .= ("application/json; charset=utf-8" :: Text),
@@ -136,15 +138,14 @@ spec = do
136138 [ LM.partBS "foo" "foo data!",
137139 LM.partBS "bar" "bar data!"
138140 ]
139− r <- req POST (httpbun /: "post") body jsonResponse mempty
141+ r <- req POST (httpbin /: "post") body jsonResponse mempty
140142 let contentType = fromJust (getRequestContentType body)
141143 (stripFunnyHeaders . stripOrigin) (responseBody r)
142144 `shouldBe` object
143145 [ "args" .= emptyObject,
144146 "json" .= Null,
145147 "data" .= ("" :: Text),
146− "url" .= ("https://httpbun.org/post" :: Text),
147− "method" .= ("POST" :: Text),
148+ "url" .= ("http://localhost:1234/post" :: Text),
148149 "headers"
149150 .= object
150151 [ "Content-Type" .= T.decodeUtf8 contentType,
@@ -165,16 +166,15 @@ spec = do
165166 describe "receiving PATCHed file back" $
166167 it "works" $ do
167168 let file :: FilePath
168− file = "httpbun-data/robots.txt"
169+ file = "httpbin-data/robots.txt"
169170 contents <- TIO.readFile file
170− r <- req PATCH (httpbun /: "patch") (ReqBodyFile file) jsonResponse mempty
171+ r <- req PATCH (httpbin /: "patch") (ReqBodyFile file) jsonResponse mempty
171172 (stripFunnyHeaders . stripOrigin) (responseBody r)
172173 `shouldBe` object
173174 [ "args" .= emptyObject,
174175 "json" .= Null,
175176 "data" .= contents,
176− "url" .= ("https://httpbun.org/patch" :: Text),
177− "method" .= ("PATCH" :: Text),
177+ "url" .= ("http://localhost:1234/patch" :: Text),
178178 "headers"
179179 .= object
180180 [ "Accept-Encoding" .= ("gzip" :: Text),
@@ -193,14 +193,13 @@ spec = do
193193 "foo" =: ("bar" :: Text)
194194 <> "baz" =: (5 :: Int)
195195 <> queryFlag "quux"
196− r <- req PUT (httpbun /: "put") (ReqBodyUrlEnc params) jsonResponse mempty
196+ r <- req PUT (httpbin /: "put") (ReqBodyUrlEnc params) jsonResponse mempty
197197 (stripFunnyHeaders . stripOrigin) (responseBody r)
198198 `shouldBe` object
199199 [ "args" .= emptyObject,
200200 "json" .= Null,
201201 "data" .= ("" :: Text),
202− "url" .= ("https://httpbun.org/put" :: Text),
203− "method" .= ("PUT" :: Text),
202+ "url" .= ("http://localhost:1234/put" :: Text),
204203 "headers"
205204 .= object
206205 [ "Content-Type" .= ("application/x-www-form-urlencoded" :: Text),
@@ -219,19 +218,24 @@ spec = do
219218 responseStatusCode r `shouldBe` 200
220219 responseStatusMessage r `shouldBe` "OK"
221220
222− -- describe "receiving UTF-8 encoded Unicode data" $
223− -- it "works" $ do
224− -- r <-
225− -- req
226− -- GET
227− -- (httpbun /: "encoding" /: "utf8")
228− -- NoReqBody
229− -- bsResponse
230− -- mempty
231− -- utf8data <- B.readFile "httpbun-data/utf8.html"
232− -- responseBody r `shouldBe` utf8data
233− -- responseStatusCode r `shouldBe` 200
234− -- responseStatusMessage r `shouldBe` "OK"
221+ -- TODO /delete
222+
223+ describe "receiving UTF-8 encoded Unicode data" $
224+ it "works" $ do
225+ r <-
226+ req
227+ GET
228+ (httpbin /: "encoding" /: "utf8")
229+ NoReqBody
230+ bsResponse
231+ mempty
232+ utf8data <- B.readFile "httpbin-data/utf8.html"
233+ responseBody r `shouldBe` utf8data
234+ responseStatusCode r `shouldBe` 200
235+ responseStatusMessage r `shouldBe` "OK"
236+
237+ -- TODO /gzip
238+ -- TODO /deflate
235239
236240 describe "retrying" $
237241 it "retries as many times as specified" $ do
@@ -242,7 +246,7 @@ spec = do
242246 prepareForShit $
243247 req
244248 GET
245− (httpbun /: "status" /~ status)
249+ (httpbin /: "status" /~ status)
246250 NoReqBody
247251 ignoreResponse
248252 mempty
@@ -254,18 +258,25 @@ spec = do
254258 forM_ [400 .. 431] checkStatusCode
255259 forM_ [500 .. 511] checkStatusCode
256260
261+ -- TODO /response-headers
262+ -- TODO /redirect
263+
257264 describe "redirects" $
258265 it "follows redirects" $ do
259266 r <-
260267 req
261268 GET
262− (httpbun /: "redirect-to")
269+ (httpbin /: "redirect-to")
263270 NoReqBody
264271 ignoreResponse
265− ("url" =: ("https://httpbun.org" :: Text))
272+ ("url" =: ("https://httpbin.org" :: Text))
266273 responseStatusCode r `shouldBe` 200
267274 responseStatusMessage r `shouldBe` "OK"
268275
276+ -- TODO /relative-redicet
277+ -- TODO /absolute-redirect
278+ -- TODO /cookies
279+
269280 describe "basicAuth" $ do
270281 let user, password :: Text
271282 user = "Scooby"
@@ -276,32 +287,43 @@ spec = do
276287 prepareForShit $
277288 req
278289 GET
279− (httpbun /: "basic-auth" /~ user /~ password)
290+ (httpbin /: "basic-auth" /~ user /~ password)
280291 NoReqBody
281292 ignoreResponse
282293 mempty
283294 responseStatusCode r `shouldBe` 401
284− responseStatusMessage r `shouldBe` "Unauthorized"
295+ responseStatusMessage r `shouldBe` "UNAUTHORIZED"
285296 context "when we provide appropriate basic auth data" $
286297 it "succeeds" $ do
287298 r <-
288299 req
289300 GET
290− (httpbun /: "basic-auth" /~ user /~ password)
301+ (httpbin /: "basic-auth" /~ user /~ password)
291302 NoReqBody
292303 ignoreResponse
293− (basicAuth (T.encodeUtf8 user) (T.encodeUtf8 password))
304+ (basicAuthUnsafe (T.encodeUtf8 user) (T.encodeUtf8 password))
294305 responseStatusCode r `shouldBe` 200
295306 responseStatusMessage r `shouldBe` "OK"
296307
308+ -- TODO /hidden-basic-auth
309+ -- TODO /digest-auth
310+ -- TODO /stream
311+ -- TODO /delay
312+ -- TODO /drip
313+ -- TODO /range
314+ -- TODO /html
315+
297316 describe "robots.txt" $
298317 it "works" $ do
299− r <- req GET (httpbun /: "robots.txt") NoReqBody bsResponse mempty
300− robots <- B.readFile "httpbun-data/robots.txt"
318+ r <- req GET (httpbin /: "robots.txt") NoReqBody bsResponse mempty
319+ robots <- B.readFile "httpbin-data/robots.txt"
301320 responseBody r `shouldBe` robots
302321 responseStatusCode r `shouldBe` 200
303322 responseStatusMessage r `shouldBe` "OK"
304323
324+ -- TODO /deny
325+ -- TODO /cache
326+
305327 describe "getting random bytes" $ do
306328 it "works" $
307329 property $ \n' -> do
@@ -310,7 +332,7 @@ spec = do
310332 r <-
311333 req
312334 GET
313− (httpbun /: "bytes" /~ n)
335+ (httpbin /: "bytes" /~ n)
314336 NoReqBody
315337 lbsResponse
316338 mempty
@@ -326,27 +348,36 @@ spec = do
326348 n = 1000
327349 req
328350 GET
329− (httpbun /: "bytes" /~ n)
351+ (httpbin /: "bytes" /~ n)
330352 NoReqBody
331353 (Proxy :: Proxy (JsonResponse Value))
332354 mempty
333355 `shouldThrow` selector
334356
335−-- describe "streaming random bytes" $
336−-- it "works" $
337−-- property $ \n' -> do
338−-- let n :: Word
339−-- n = getSmall n'
340−-- r <-
341−-- req
342−-- GET
343−-- (httpbun /: "stream-bytes" /~ n)
344−-- NoReqBody
345−-- bsResponse
346−-- mempty
347−-- responseBody r `shouldSatisfy` ((== n) . fromIntegral . B.length)
348−-- responseStatusCode r `shouldBe` 200
349−-- responseStatusMessage r `shouldBe` "OK"
357+ describe "streaming random bytes" $
358+ it "works" $
359+ property $ \n' -> do
360+ let n :: Word
361+ n = getSmall n'
362+ r <-
363+ req
364+ GET
365+ (httpbin /: "stream-bytes" /~ n)
366+ NoReqBody
367+ bsResponse
368+ mempty
369+ responseBody r `shouldSatisfy` ((== n) . fromIntegral . B.length)
370+ responseStatusCode r `shouldBe` 200
371+ responseStatusMessage r `shouldBe` "OK"
372+
373+-- TODO /links
374+-- TODO /image
375+-- TODO /image/png
376+-- TODO /image/jpeg
377+-- TODO /image/webp
378+-- TODO /image/svg
379+-- TODO /forms/post
380+-- TODO /xml
350381
351382----------------------------------------------------------------------------
352383-- Instances
@@ -369,9 +400,29 @@ blindlyThrowing = runReq defaultHttpConfig {httpConfigCheckResponse = doit}
369400 where
370401 doit _ _ = error "Oops!"
371402
372−-- | 'Url' representing <https://httpbun.org>.
373−httpbun :: Url 'Https
374−httpbun = https "httpbun.org"
403+-- | 'Url' representing <https://httpbin.org>.
404+httpbin :: Url 'Http
405+httpbin = http "localhost"
406+
407+req ::
408+ ( MonadHttp m,
409+ HttpMethod method,
410+ HttpBody body,
411+ HttpResponse response,
412+ HttpBodyAllowed (AllowsBody method) (ProvidesBody body)
413+ ) =>
414+ method ->
415+ Url scheme ->
416+ body ->
417+ Proxy response ->
418+ Option scheme ->
419+ m response
420+req method url body responseProxy options =
421+ Req.req method url body responseProxy (options <> defaultOptions)
422+
423+-- | Options to apply by default.
424+defaultOptions :: Option scheme
425+defaultOptions = port 1234
375426
376427-- | Remove “origin” field from JSON value. Origin may change, we don't want
377428-- to depend on that.
@@ -412,7 +463,7 @@ stripFunnyHeaders' = \case
412463 "Baz"
413464 ]
414465
415−-- | This is a complete test case that makes use of <https://httpbun.org> to
466+-- | This is a complete test case that makes use of <https://httpbin.org> to
416467-- get various response status codes.
417468checkStatusCode :: Int -> SpecWith ()
418469checkStatusCode code =
@@ -422,7 +473,7 @@ checkStatusCode code =
422473 prepareForShit $
423474 req
424475 GET
425− (httpbun /: "status" /~ code)
476+ (httpbin /: "status" /~ code)
426477 NoReqBody
427478 ignoreResponse
428479 mempty