Skip to content

Commit dc7e5af

Browse files
committed
Refactor making requests for BwdServer.Tests
1 parent 4a65244 commit dc7e5af

File tree

1 file changed

+61
-52
lines changed

1 file changed

+61
-52
lines changed

fsharp-backend/tests/Tests/BwdServer.Tests.fs

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module RT = LibExecution.RuntimeTypes
1616
module PT = LibExecution.ProgramTypes
1717
module Routing = LibBackend.Routing
1818
module Canvas = LibBackend.Canvas
19+
module DvalRepr = LibExecution.DvalReprExternal
1920

2021
open TestUtils.TestUtils
2122
open System.Text.Json
@@ -292,48 +293,10 @@ let t filename =
292293
return client
293294
}
294295

295-
let callServer (server : Server) : Task<unit> =
296+
let makeRequest (request : byte array) (port : int) : Task<Http.T> =
296297
task {
297-
let port =
298-
match server with
299-
| OCaml -> TestConfig.ocamlServerNginxPort
300-
| FSharp -> TestConfig.bwdServerBackendPort
301-
302-
let host = $"{meta.name}.builtwithdark.localhost:{port}"
303-
let canvasName = string meta.name
304-
305-
let request =
306-
test.request
307-
|> replaceByteStrings "HOST" host
308-
|> replaceByteStrings "CANVAS" canvasName
309-
|> Http.setHeadersToCRLF
310-
311-
// Check body matches content-length
312-
let incorrectContentTypeAllowed =
313-
test.request
314-
|> UTF8.ofBytesWithReplacement
315-
|> String.includes "ALLOW-INCORRECT-CONTENT-LENGTH"
316-
317-
if not incorrectContentTypeAllowed then
318-
let parsedTestRequest = Http.split request
319-
let contentLength =
320-
parsedTestRequest.headers
321-
|> List.find (fun (k, v) -> String.toLowercase k = "content-length")
322-
match contentLength with
323-
| None -> ()
324-
| Some (_, v) ->
325-
if String.includes "ALLOW-INCORRECT-CONTENT-LENGTH" v then
326-
()
327-
else
328-
Expect.equal parsedTestRequest.body.Length (int v) ""
329-
330-
// Check input LENGTH not set
331-
if test.request |> UTF8.ofBytesWithReplacement |> String.includes "LENGTH"
332-
&& not incorrectContentTypeAllowed then // false alarm as also have LENGTH in it
333-
Expect.isFalse true "LENGTH substitution not done on request"
334-
335298
// Make a request
336-
use! client = createClient (port)
299+
use! client = createClient port
337300
use stream = client.GetStream()
338301
stream.ReadTimeout <- 1000 // responses should be instant, right?
339302

@@ -348,8 +311,58 @@ let t filename =
348311
client.Close()
349312
let response = Array.take byteCount responseBuffer
350313

314+
return Http.split response
315+
}
316+
317+
let prepareRequest (host : string) (canvasName : string) : byte array =
318+
let request =
319+
test.request
320+
|> replaceByteStrings "HOST" host
321+
|> replaceByteStrings "CANVAS" canvasName
322+
|> Http.setHeadersToCRLF
323+
324+
// Check body matches content-length
325+
let incorrectContentTypeAllowed =
326+
test.request
327+
|> UTF8.ofBytesWithReplacement
328+
|> String.includes "ALLOW-INCORRECT-CONTENT-LENGTH"
329+
330+
if not incorrectContentTypeAllowed then
331+
let parsedTestRequest = Http.split request
332+
let contentLength =
333+
parsedTestRequest.headers
334+
|> List.find (fun (k, v) -> String.toLowercase k = "content-length")
335+
match contentLength with
336+
| None -> ()
337+
| Some (_, v) ->
338+
if String.includes "ALLOW-INCORRECT-CONTENT-LENGTH" v then
339+
()
340+
else
341+
Expect.equal parsedTestRequest.body.Length (int v) ""
342+
343+
// Check input LENGTH not set
344+
if test.request |> UTF8.ofBytesWithReplacement |> String.includes "LENGTH"
345+
&& not incorrectContentTypeAllowed then // false alarm as also have LENGTH in it
346+
Expect.isFalse true "LENGTH substitution not done on request"
347+
348+
request
349+
350+
let callServer (server : Server) : Task<unit> =
351+
task {
352+
let port =
353+
match server with
354+
| OCaml -> TestConfig.ocamlServerNginxPort
355+
| FSharp -> TestConfig.bwdServerBackendPort
356+
357+
let host = $"{meta.name}.builtwithdark.localhost:{port}"
358+
print $"{host}"
359+
let canvasName = string meta.name
360+
361+
let request = prepareRequest host canvasName
362+
let! actual = makeRequest request port
363+
351364
// Prepare expected response
352-
let expectedResponse =
365+
let expected =
353366
test.response
354367
|> splitAtNewlines
355368
|> List.filterMap (fun line ->
@@ -378,35 +391,31 @@ let t filename =
378391
|> replaceByteStrings "HOST" host
379392
|> replaceByteStrings "CANVAS" canvasName
380393
|> Http.setHeadersToCRLF
394+
|> Http.split
381395

382396
// Parse and normalize the response
383-
let actual = Http.split response
384-
let expected = Http.split expectedResponse
385397
let eHeaders = normalizeExpectedHeaders expected.headers actual.body
386398
let aHeaders = normalizeActualHeaders actual.headers
387399

388-
// Decompress the body if returned with a content-encoding. Throws an exception if content-encoding is set and the body is not encrypted. This lets us test that the server returns compressed content
400+
// Decompress the body if returned with a content-encoding. Throws an
401+
// exception if content-encoding is set and the body is not compressed. This
402+
// lets us test that the server returns compressed content
389403
let actual =
390404
{ actual with body = Http.decompressIfNeeded actual.headers actual.body }
391405

392406
// Test as json or strings
393407
let asJson =
394408
try
395409
Some(
396-
LibExecution.DvalReprExternal.parseJson (
397-
UTF8.ofBytesUnsafe actual.body
398-
),
399-
LibExecution.DvalReprExternal.parseJson (
400-
UTF8.ofBytesUnsafe expected.body
401-
)
410+
DvalRepr.parseJson (UTF8.ofBytesUnsafe actual.body),
411+
DvalRepr.parseJson (UTF8.ofBytesUnsafe expected.body)
402412
)
403413
with
404414
| e -> None
405415

406416
match asJson with
407417
| Some (aJson, eJson) ->
408-
let serialize (json : JsonDocument) =
409-
LibExecution.DvalReprExternal.writePrettyJson json.WriteTo
418+
let serialize (json : JsonDocument) = DvalRepr.writePrettyJson json.WriteTo
410419
Expect.equal
411420
(actual.status, aHeaders, serialize aJson)
412421
(expected.status, eHeaders, serialize eJson)

0 commit comments

Comments
 (0)