Skip to content

Commit 17885c3

Browse files
committed
Adapt HighLevelTest to user-content host split
1 parent 19b15bf commit 17885c3

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

tests/HackageClientUtils.hs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ withServerRunning root f
5252
info "Finished with server")
5353

5454
serverRunningArgs :: [String]
55-
serverRunningArgs = ["run", "--ip", "127.0.0.1", "--port", show testPort, "--delay-cache-updates", "0"]
55+
serverRunningArgs =
56+
["run", "--ip", "127.0.0.1"
57+
, "--port", show testPort
58+
, "--delay-cache-updates", "0"
59+
, "--base-uri", "http://127.0.0.1:" <> show testPort
60+
, "--user-content-uri", "http://localhost:" <> show testPort
61+
, "--required-base-host-header", "127.0.0.1:" <> show testPort
62+
]
5663

5764
waitForServer :: IO ()
5865
waitForServer = f 10
@@ -261,9 +268,15 @@ testPort = 8392
261268
mkUrl :: RelativeURL -> AbsoluteURL
262269
mkUrl relPath = "http://127.0.0.1:" ++ show testPort ++ relPath
263270

271+
mkUserContentUrl :: RelativeURL -> AbsoluteURL
272+
mkUserContentUrl relPath = "http://localhost:" ++ show testPort ++ relPath
273+
264274
mkGetReq :: RelativeURL -> Request_String
265275
mkGetReq url = getRequest (mkUrl url)
266276

277+
mkGetUserContentReq :: RelativeURL -> Request_String
278+
mkGetUserContentReq url = getRequest (mkUserContentUrl url)
279+
267280
mkPostReq :: RelativeURL -> [(String, String)] -> Request_String
268281
mkPostReq url vals =
269282
setRequestBody (postRequest (mkUrl url))
@@ -295,15 +308,27 @@ putRequest urlString =
295308
getUrl :: Authorization -> RelativeURL -> IO String
296309
getUrl auth url = Http.execRequest auth (mkGetReq url)
297310

311+
getUserContentUrl :: Authorization -> RelativeURL -> IO String
312+
getUserContentUrl auth url = Http.execRequest auth (mkGetUserContentReq url)
313+
298314
getETag :: RelativeURL -> IO String
299315
getETag url = Http.responseHeader HdrETag (mkGetReq url)
300316

317+
getETagUserContent :: RelativeURL -> IO String
318+
getETagUserContent url = Http.responseHeader HdrETag (mkGetUserContentReq url)
319+
301320
mkGetReqWithETag :: String -> RelativeURL -> Request_String
302321
mkGetReqWithETag url etag =
303322
Request (fromJust $ parseURI $ mkUrl url) GET hdrs ""
304323
where
305324
hdrs = [mkHeader HdrIfNoneMatch etag]
306325

326+
mkGetUserContentReqWithETag :: String -> RelativeURL -> Request_String
327+
mkGetUserContentReqWithETag url etag =
328+
Request (fromJust $ parseURI $ mkUserContentUrl url) GET hdrs ""
329+
where
330+
hdrs = [mkHeader HdrIfNoneMatch etag]
331+
307332
validateETagHandling :: RelativeURL -> IO ()
308333
validateETagHandling url = void $ do
309334
etag <- getETag url
@@ -313,6 +338,15 @@ validateETagHandling url = void $ do
313338
checkETag etag = void $ Http.execRequest' NoAuth (mkGetReqWithETag url etag) isNotModified
314339
checkETagMismatch etag = void $ Http.execRequest NoAuth (mkGetReqWithETag url etag)
315340

341+
validateETagHandlingUserContent :: RelativeURL -> IO ()
342+
validateETagHandlingUserContent url = void $ do
343+
etag <- getETagUserContent url
344+
checkETag etag
345+
checkETagMismatch (etag ++ "garbled123")
346+
where
347+
checkETag etag = void $ Http.execRequest' NoAuth (mkGetUserContentReqWithETag url etag) isNotModified
348+
checkETagMismatch etag = void $ Http.execRequest NoAuth (mkGetUserContentReqWithETag url etag)
349+
316350
getJSONStrings :: RelativeURL -> IO [String]
317351
getJSONStrings url = getUrl NoAuth url >>= decodeJSON
318352

tests/HighLevelTest.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,11 @@ runPackageTests = do
243243
unless (tarFile == testpackageTarFileContent) $
244244
die "Bad tar file"
245245
do info "Getting testpackage source"
246-
hsFile <- getUrl NoAuth ("/package/testpackage/src" </> testpackageHaskellFilename)
246+
hsFile <- getUserContentUrl NoAuth ("/package/testpackage/src" </> testpackageHaskellFilename)
247247
unless (hsFile == testpackageHaskellFileContent) $
248248
die "Bad Haskell file"
249249
do info "Getting testpackage source with etag"
250-
validateETagHandling ("/package/testpackage/src" </> testpackageHaskellFilename)
250+
validateETagHandlingUserContent ("/package/testpackage/src" </> testpackageHaskellFilename)
251251
do info "Getting testpackage maintainer info"
252252
xs <- getGroup "/package/testpackage/maintainers/.json"
253253
unless (map userName (groupMembers xs) == ["HackageTestUser1"]) $

tests/HttpUtils.hs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ module HttpUtils (
2525
import Control.Exception
2626
import Control.Monad
2727
import Data.Maybe
28+
import Data.Text (unpack)
29+
import qualified Data.Text.Encoding as Enc
2830
import Network.HTTP hiding (user)
2931
import Network.HTTP.Auth
3032
import Data.Aeson (Result(..), Value(..), FromJSON(..), (.:), fromJSON)
@@ -242,7 +244,19 @@ jsonHandler :: FromJSON a
242244
-> Streams.InputStream BS.ByteString
243245
-> IO a
244246
jsonHandler _ i = do
245-
v <- Streams.parseFromStream json' i
246-
case fromJSON v of
247-
(Success a) -> return a
248-
(Error str) -> fail str
247+
-- Note that this might not read the _whole_ input
248+
-- But the beginning is often good enough for diagnosing the failure
249+
mbByteString <- Streams.read i
250+
forM_ mbByteString $ \bs -> Streams.unRead bs i
251+
eitherV <- try $ Streams.parseFromStream json' i
252+
case fromJSON <$> eitherV of
253+
Left ex -> do
254+
let
255+
_ex :: SomeException
256+
_ex = ex
257+
forM_ mbByteString $ \bs ->
258+
forM_ (Enc.decodeUtf8' bs) $ \text ->
259+
putStrLn (unpack text)
260+
fail "Response was not JSON"
261+
Right (Success a) -> return a
262+
Right (Error str) -> fail str

0 commit comments

Comments
 (0)