Skip to content

Commit

Permalink
v0.0.12
Browse files Browse the repository at this point in the history
- Response.body_() added
- fix bug for rejected Promise in resolve_response()
  • Loading branch information
hnry committed Aug 1, 2016
1 parent 8ddaaf2 commit 9e1175d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "spirit",
"version": "0.0.11",
"version": "0.0.12",
"description": "extensible web library for building applications & frameworks",
"main": "index.js",
"scripts": {
Expand Down
19 changes: 17 additions & 2 deletions spec/http/utils-spec.js
Expand Up @@ -29,7 +29,7 @@ describe("resolve_response", () => {
})
})

it("returns the promise passed in if it's resolved value is a response map but non-promise body", (done) => {
it("returns the promise passed in, if it's resolved value is a response map but non-promise body", (done) => {
const p = Promise.resolve({
status: 123,
headers: {a:1},
Expand All @@ -46,13 +46,28 @@ describe("resolve_response", () => {
})
})

it("returns the promise passed in if it's resolved value is not a response map", (done) => {
it("returns the promise passed in, if it's resolved value is not a response map", (done) => {
const p = Promise.resolve(123)
resolve(p).then((result) => {
expect(result).toBe(123)
done()
})
})

it("if the response body is a rejected promise, it ignores the responses and returns the rejected promise", (done) => {
const p = Promise.reject("error")

const resp = {
status: 123,
headers: {a:1},
body: p
}

resolve(Promise.resolve(resp)).catch((err) => {
expect(err).toBe("error")
done()
})
})
})

describe("size_of", () => {
Expand Down
14 changes: 10 additions & 4 deletions src/core/promise_utils.js
Expand Up @@ -58,10 +58,16 @@ const resolve_response = (p) => {
if (is_response(result)
&& is_promise(result.body)) {
return new Promise((resolve, reject) => {
result.body.then((body) => {
result.body = body
resolve(result)
})
result.body
.then((body) => {
result.body = body
resolve(result)
})
.catch((err) => {
// if the body is rejected Promise
// throw the error of the body instead of the resp
reject(err)
})
})
}
// otherwise just return
Expand Down

0 comments on commit 9e1175d

Please sign in to comment.