Skip to content

Commit

Permalink
Http bytes compatibility (#26)
Browse files Browse the repository at this point in the history
* Adds elm/bytes compatibility for ConcurrentTask.Http

* Adds withMetadata utility
  • Loading branch information
andrewMacmurray authored Dec 17, 2023
1 parent 516ab3a commit d0d3f46
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 14 deletions.
2 changes: 2 additions & 0 deletions elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
],
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {
"danfishgold/base64-bytes": "1.1.0 <= v < 2.0.0",
"elm/browser": "1.0.2 <= v < 2.0.0",
"elm/bytes": "1.0.8 <= v < 2.0.0",
"elm/core": "1.0.5 <= v < 2.0.0",
"elm/json": "1.1.3 <= v < 2.0.0",
"elm/random": "1.0.0 <= v < 2.0.0",
Expand Down
2 changes: 2 additions & 0 deletions integration/elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"danfishgold/base64-bytes": "1.1.0",
"elm/browser": "1.0.2",
"elm/bytes": "1.0.8",
"elm/core": "1.0.5",
"elm/html": "1.0.0",
"elm/json": "1.1.3",
Expand Down
2 changes: 2 additions & 0 deletions integration/scripts/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const PORT = process.env.PORT || 4999;

app.use(morgan("tiny"));
app.use(express.json());
app.use(express.raw({ type: "application/octet-stream" }));

app.get("/wait-then-respond/:time", (req, res) => {
setTimeout(() => {
Expand All @@ -14,6 +15,7 @@ app.get("/wait-then-respond/:time", (req, res) => {
});

app.post("/echo", (req, res) => {
res.setHeaders(new Headers(req.headers))
res.send(req.body);
});

Expand Down
66 changes: 66 additions & 0 deletions integration/src/Main.elm
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
port module Main exposing (main)

import Bytes
import Bytes.Decode
import Bytes.Encode
import ConcurrentTask as Task exposing (ConcurrentTask, UnexpectedError(..))
import ConcurrentTask.Http as Http
import ConcurrentTask.Process
import ConcurrentTask.Random
import ConcurrentTask.Time
import Dict
import Integration.Runner as Runner exposing (RunnerProgram)
import Integration.Spec as Spec exposing (Spec)
import Json.Decode as Decode exposing (Decoder)
Expand Down Expand Up @@ -39,6 +43,8 @@ specs =
, complexResponseSpec
, missingFunctionSpec
, httpJsonBodySpec
, httpHeadersSpec
, httpBytesSpec
, httpMalformedSpec
, httpStringSpec
, httpTimeoutSpec
Expand Down Expand Up @@ -225,6 +231,38 @@ httpJsonBodySpec =
)


httpBytesSpec : Spec
httpBytesSpec =
let
body : Bytes.Encode.Encoder
body =
Bytes.Encode.sequence
[ Bytes.Encode.unsignedInt32 Bytes.BE 41
, Bytes.Encode.unsignedInt32 Bytes.BE 1
]

response : Bytes.Decode.Decoder Int
response =
Bytes.Decode.map2 (+)
(Bytes.Decode.unsignedInt32 Bytes.BE)
(Bytes.Decode.unsignedInt32 Bytes.BE)
in
Spec.describe
"http bytes"
"sends http bytes body in a request and decodes them in response"
(Http.post
{ url = echoBody
, headers = []
, timeout = Nothing
, expect = Http.expectBytes response
, body = Http.bytesBody "application/octet-stream" (Bytes.Encode.encode body)
}
)
(Spec.assertSuccess
(Spec.shouldEqual 42)
)


httpMalformedSpec : Spec
httpMalformedSpec =
Spec.describe
Expand All @@ -242,6 +280,34 @@ httpMalformedSpec =
)


httpHeadersSpec : Spec
httpHeadersSpec =
Spec.describe
"http headers"
"should send and receive http headers"
(Http.post
{ url = echoBody
, headers = [ Http.header "foo" "bar" ]
, expect = Http.withMetadata always Http.expectWhatever
, timeout = Nothing
, body = Http.emptyBody
}
)
(Spec.assertSuccess
(\meta ->
case Dict.get "foo" meta.headers of
Just "bar" ->
Spec.pass

Just x ->
Spec.failWith "Got a header but not the expected value" x

Nothing ->
Spec.failWith "Did not contain expected header" meta
)
)


httpStringSpec : Spec
httpStringSpec =
Spec.describe
Expand Down
14 changes: 14 additions & 0 deletions runner/http/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ export function http(request: HttpRequest): Promise<HttpResponse> {
body: x || null,
}));
}
case "BYTES": {
return res
.blob()
.then((blob) => blob.text())
.then((x) => {
return {
url: res.url,
headers: headers,
statusCode: res.status,
statusText: res.statusText,
body: x || null,
};
});
}
case "WHATEVER": {
return {
url: res.url,
Expand Down
2 changes: 1 addition & 1 deletion runner/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface HttpRequest {
}

export type HttpResponse = ResponseSuccess | ResponseError;
export type Expect = "STRING" | "JSON" | "WHATEVER";
export type Expect = "STRING" | "JSON" | "BYTES" | "WHATEVER";

export interface ResponseSuccess {
body: string | null;
Expand Down
Loading

0 comments on commit d0d3f46

Please sign in to comment.