Skip to content

Commit

Permalink
🐛 Take into account the options argument headers when stringifying bo…
Browse files Browse the repository at this point in the history
…dy. #75
  • Loading branch information
elbywan committed Jun 30, 2021
1 parent 69800b4 commit 23484fd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
19 changes: 11 additions & 8 deletions src/wretcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,23 @@ export class Wretcher {
}

private method(method: string, options = {}, body = null) {
const headers = this._options.headers
let baseWretcher =
!body ? this :
let base = this.options({ ...options, method })
const headers = base._options.headers
base =
!body ? base :
typeof body === "object" && (
!headers ||
Object.entries(headers).every(([k, v]) =>
k.toLowerCase() !== CONTENT_TYPE_HEADER.toLowerCase() ||
v.startsWith(JSON_MIME)
)
) ? this.json(body) :
this.body(body)
baseWretcher = baseWretcher.options({ ...options, method })
const deferredWretcher = baseWretcher._deferredChain.reduce((acc: Wretcher, curr) => curr(acc, acc._url, acc._options), baseWretcher)
return resolver(deferredWretcher)
) ? base.json(body) :
base.body(body)
return resolver(
base
._deferredChain
.reduce((acc: Wretcher, curr) => curr(acc, acc._url, acc._options), base)
)
}

/**
Expand Down
14 changes: 14 additions & 0 deletions test/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ const mockServer = {
}
})

server.post("/blob/roundTrip", (req, res) => {
try {
if(req.header("content-type") === "application/xxx-octet-stream") {
res.header("content-type", "application/octet-stream")
res.sendRaw(req.body)
} else {
res.send(400)
}
} catch (error) {
console.error(error)
res.send(400)
}
})

server.post("/formData/decode", (req, res) => {
if(req.header("content-type").startsWith("multipart/form-data"))
res.json(req.params)
Expand Down
34 changes: 27 additions & 7 deletions test/node/wretch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import * as path from "path"
import wretch from "../../src"
import { mix } from "../../src/mix"

const { performance, PerformanceObserver } = require("perf_hooks")
// tslint:disable-next-line:no-empty
performance.clearResourceTimings = () => {}
import { performance, PerformanceObserver } from "perf_hooks"
performance["clearResourceTimings"] = () => {}

const _PORT = 9876
const _URL = `http://localhost:${_PORT}`
Expand All @@ -26,15 +25,14 @@ const fetchPolyfill = (timeout = null) =>
function (url, opts) {
performance.mark(url + " - begin")
const { fetch } = abortableFetch(nodeFetch) as any
return fetch(url, opts).then(_ => {
return fetch(url, opts).then(res => {
performance.mark(url + " - end")
const measure = () => performance.measure(_.url, url + " - begin", url + " - end")
performance.clearMarks(url + " - begin")
const measure = () => performance.measure(res.url, url + " - begin", url + " - end")
if(timeout)
setTimeout(measure, timeout)
else
measure()
return _
return res
})
}

Expand Down Expand Up @@ -90,6 +88,28 @@ describe("Wretch", function () {
await allRoutes(init, "blob", test, {}, {})
})

it("should not stringify a blob when the content-type is not json", async function () {
expect((
await wretch(`${_URL}/blob/roundTrip`, {
// using 'xxx-' prefix because otherwise node-fetch sends an empty body
headers: { "content-type": "application/xxx-octet-stream" }
})
.post(duckImage)
.res(res => res["buffer"]() as Buffer))
.compare(duckImage)
).toBe(0)

// Headers are set in the options argument of the http method
expect((
await wretch(`${_URL}/blob/roundTrip`)
.post(duckImage, {
headers: { "content-type":"application/xxx-octet-stream" }
})
.res(res => res["buffer"]() as Buffer)
).compare(duckImage)
).toBe(0)
})

it("should perform crud requests and parse an arrayBuffer response", async function () {
const test = arrayBuffer => {
const buffer = Buffer.alloc(arrayBuffer.byteLength)
Expand Down

0 comments on commit 23484fd

Please sign in to comment.