Skip to content

Commit

Permalink
Refactor making requests for BwdServer.Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pbiggar committed Apr 8, 2022
1 parent 4a65244 commit dc7e5af
Showing 1 changed file with 61 additions and 52 deletions.
113 changes: 61 additions & 52 deletions fsharp-backend/tests/Tests/BwdServer.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module RT = LibExecution.RuntimeTypes
module PT = LibExecution.ProgramTypes
module Routing = LibBackend.Routing
module Canvas = LibBackend.Canvas
module DvalRepr = LibExecution.DvalReprExternal

open TestUtils.TestUtils
open System.Text.Json
Expand Down Expand Up @@ -292,48 +293,10 @@ let t filename =
return client
}

let callServer (server : Server) : Task<unit> =
let makeRequest (request : byte array) (port : int) : Task<Http.T> =
task {
let port =
match server with
| OCaml -> TestConfig.ocamlServerNginxPort
| FSharp -> TestConfig.bwdServerBackendPort

let host = $"{meta.name}.builtwithdark.localhost:{port}"
let canvasName = string meta.name

let request =
test.request
|> replaceByteStrings "HOST" host
|> replaceByteStrings "CANVAS" canvasName
|> Http.setHeadersToCRLF

// Check body matches content-length
let incorrectContentTypeAllowed =
test.request
|> UTF8.ofBytesWithReplacement
|> String.includes "ALLOW-INCORRECT-CONTENT-LENGTH"

if not incorrectContentTypeAllowed then
let parsedTestRequest = Http.split request
let contentLength =
parsedTestRequest.headers
|> List.find (fun (k, v) -> String.toLowercase k = "content-length")
match contentLength with
| None -> ()
| Some (_, v) ->
if String.includes "ALLOW-INCORRECT-CONTENT-LENGTH" v then
()
else
Expect.equal parsedTestRequest.body.Length (int v) ""

// Check input LENGTH not set
if test.request |> UTF8.ofBytesWithReplacement |> String.includes "LENGTH"
&& not incorrectContentTypeAllowed then // false alarm as also have LENGTH in it
Expect.isFalse true "LENGTH substitution not done on request"

// Make a request
use! client = createClient (port)
use! client = createClient port
use stream = client.GetStream()
stream.ReadTimeout <- 1000 // responses should be instant, right?

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

return Http.split response
}

let prepareRequest (host : string) (canvasName : string) : byte array =
let request =
test.request
|> replaceByteStrings "HOST" host
|> replaceByteStrings "CANVAS" canvasName
|> Http.setHeadersToCRLF

// Check body matches content-length
let incorrectContentTypeAllowed =
test.request
|> UTF8.ofBytesWithReplacement
|> String.includes "ALLOW-INCORRECT-CONTENT-LENGTH"

if not incorrectContentTypeAllowed then
let parsedTestRequest = Http.split request
let contentLength =
parsedTestRequest.headers
|> List.find (fun (k, v) -> String.toLowercase k = "content-length")
match contentLength with
| None -> ()
| Some (_, v) ->
if String.includes "ALLOW-INCORRECT-CONTENT-LENGTH" v then
()
else
Expect.equal parsedTestRequest.body.Length (int v) ""

// Check input LENGTH not set
if test.request |> UTF8.ofBytesWithReplacement |> String.includes "LENGTH"
&& not incorrectContentTypeAllowed then // false alarm as also have LENGTH in it
Expect.isFalse true "LENGTH substitution not done on request"

request

let callServer (server : Server) : Task<unit> =
task {
let port =
match server with
| OCaml -> TestConfig.ocamlServerNginxPort
| FSharp -> TestConfig.bwdServerBackendPort

let host = $"{meta.name}.builtwithdark.localhost:{port}"
print $"{host}"
let canvasName = string meta.name

let request = prepareRequest host canvasName
let! actual = makeRequest request port

// Prepare expected response
let expectedResponse =
let expected =
test.response
|> splitAtNewlines
|> List.filterMap (fun line ->
Expand Down Expand Up @@ -378,35 +391,31 @@ let t filename =
|> replaceByteStrings "HOST" host
|> replaceByteStrings "CANVAS" canvasName
|> Http.setHeadersToCRLF
|> Http.split

// Parse and normalize the response
let actual = Http.split response
let expected = Http.split expectedResponse
let eHeaders = normalizeExpectedHeaders expected.headers actual.body
let aHeaders = normalizeActualHeaders actual.headers

// 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
// Decompress the body if returned with a content-encoding. Throws an
// exception if content-encoding is set and the body is not compressed. This
// lets us test that the server returns compressed content
let actual =
{ actual with body = Http.decompressIfNeeded actual.headers actual.body }

// Test as json or strings
let asJson =
try
Some(
LibExecution.DvalReprExternal.parseJson (
UTF8.ofBytesUnsafe actual.body
),
LibExecution.DvalReprExternal.parseJson (
UTF8.ofBytesUnsafe expected.body
)
DvalRepr.parseJson (UTF8.ofBytesUnsafe actual.body),
DvalRepr.parseJson (UTF8.ofBytesUnsafe expected.body)
)
with
| e -> None

match asJson with
| Some (aJson, eJson) ->
let serialize (json : JsonDocument) =
LibExecution.DvalReprExternal.writePrettyJson json.WriteTo
let serialize (json : JsonDocument) = DvalRepr.writePrettyJson json.WriteTo
Expect.equal
(actual.status, aHeaders, serialize aJson)
(expected.status, eHeaders, serialize eJson)
Expand Down

0 comments on commit dc7e5af

Please sign in to comment.