Skip to content

Commit 338c382

Browse files
Add expectRawBytes
This allows decoding the whole response body as raw bytes
1 parent 0bd7a48 commit 338c382

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

integration/src/Main.elm

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
port module Main exposing (main)
22

3-
import Bytes
3+
import Bytes exposing (Bytes)
44
import Bytes.Decode
55
import Bytes.Encode
66
import ConcurrentTask as Task exposing (ConcurrentTask, UnexpectedError(..))
@@ -50,6 +50,7 @@ specs =
5050
, httpJsonBodySpec
5151
, httpHeadersSpec
5252
, httpBytesSpec
53+
, httpRawBytesSpec
5354
, httpMalformedSpec
5455
, httpStringSpec
5556
, httpTimeoutSpec
@@ -313,6 +314,30 @@ httpJsonBodySpec =
313314
)
314315

315316

317+
httpRawBytesSpec : Spec
318+
httpRawBytesSpec =
319+
let
320+
body : Bytes
321+
body =
322+
Bytes.Encode.unsignedInt32 Bytes.BE 42
323+
|> Bytes.Encode.encode
324+
in
325+
Spec.describe
326+
"http raw bytes"
327+
"sends http bytes body in a request and receives them in response"
328+
(Http.post
329+
{ url = echoBody
330+
, headers = []
331+
, timeout = Nothing
332+
, expect = Http.expectRawBytes
333+
, body = Http.bytesBody "application/octet-stream" body
334+
}
335+
)
336+
(Spec.assertSuccess
337+
(Spec.shouldEqual body)
338+
)
339+
340+
316341
httpBytesSpec : Spec
317342
httpBytesSpec =
318343
let

src/ConcurrentTask/Http.elm

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module ConcurrentTask.Http exposing
22
( request, get, post
33
, Body, emptyBody, stringBody, jsonBody, bytesBody
4-
, Expect, expectJson, expectString, expectBytes, expectWhatever, withMetadata
4+
, Expect, expectJson, expectString, expectBytes, expectRawBytes, expectWhatever, withMetadata
55
, Header, header
66
, Error(..), Metadata
77
)
@@ -48,7 +48,7 @@ You could create entirely your own from scratch - maybe you want an http package
4848
4949
# Expect
5050
51-
@docs Expect, expectJson, expectString, expectBytes, expectWhatever, withMetadata
51+
@docs Expect, expectJson, expectString, expectBytes, expectRawBytes, expectWhatever, withMetadata
5252
5353
5454
# Headers
@@ -88,7 +88,7 @@ type Body
8888
type Expect a
8989
= ExpectJson (Decoder a)
9090
| ExpectString (Decoder a)
91-
| ExpectBytes (Bytes.Decode.Decoder a)
91+
| ExpectBytes (Bytes -> Bytes.Decode.Decoder a)
9292
| ExpectWhatever (Decoder a)
9393
| ExpectMetadata (Metadata -> Expect a)
9494

@@ -238,8 +238,15 @@ expectString =
238238
{-| Expect the response body to be `Bytes`, decode it using the supplied decoder.
239239
-}
240240
expectBytes : Bytes.Decode.Decoder a -> Expect a
241-
expectBytes =
242-
ExpectBytes
241+
expectBytes decoder =
242+
ExpectBytes (\_ -> decoder)
243+
244+
245+
{-| Expect the response body to be raw `Bytes`.
246+
-}
247+
expectRawBytes : Expect Bytes
248+
expectRawBytes =
249+
ExpectBytes Bytes.Decode.succeed
243250

244251

245252
{-| Discard the response body.
@@ -261,7 +268,7 @@ withMetadata toMeta expect =
261268
ExpectMetadata (\meta -> ExpectString (Decode.map (toMeta meta) decoder))
262269

263270
ExpectBytes decoder ->
264-
ExpectMetadata (\meta -> ExpectBytes (Bytes.Decode.map (toMeta meta) decoder))
271+
ExpectMetadata (\meta -> ExpectBytes (\raw -> Bytes.Decode.map (toMeta meta) (decoder raw)))
265272

266273
ExpectWhatever decoder ->
267274
ExpectMetadata (\meta -> ExpectWhatever (Decode.map (toMeta meta) decoder))
@@ -421,14 +428,14 @@ decodeJsonBody decoder meta =
421428
)
422429

423430

424-
decodeBytesBody : Bytes.Decode.Decoder a -> Metadata -> Decoder (Result Error a)
431+
decodeBytesBody : (Bytes -> Bytes.Decode.Decoder a) -> Metadata -> Decoder (Result Error a)
425432
decodeBytesBody decoder meta =
426433
Decode.string
427434
|> Decode.map
428435
(\res ->
429436
case Base64.toBytes res of
430437
Just bytes ->
431-
case Bytes.Decode.decode decoder bytes of
438+
case Bytes.Decode.decode (decoder bytes) bytes of
432439
Just a ->
433440
Ok a
434441

0 commit comments

Comments
 (0)