-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task/function to overwrite response headers (#104)
* Use setResponseStatus to overwrite response status code - rename from setResponseHeaders - add class attribute for headers and status code - if status code is set, then dont use the one generated from the message (this allows a user to overwrite the status if its not one of the ones in errorToStatus) - send response for notification in gotNotification - Fix gotNotification doc * Use .end() to send body data in response.write() and writeHead() * Fix doc for gotError * Add tests for setResponseStatus and overrides * Call messageBuffer and _waitForData when client request comes in * Update doc for clientConnected
- Loading branch information
Showing
3 changed files
with
110 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const { expect } = require("chai"); | ||
const Jaysonic = require("../../src/"); | ||
const HttpServerFactory = require("../../src/server/http"); | ||
const HttpServerProtocol = require("../../src/server/protocol/http"); | ||
|
||
class Protocol extends HttpServerProtocol { | ||
constructor(factory, client, response, version, delimiter) { | ||
super(factory, client, response, version, delimiter); | ||
this.status = 404; | ||
} | ||
} | ||
|
||
class Factory extends HttpServerFactory { | ||
constructor(options) { | ||
super(options); | ||
this.protocol = Protocol; | ||
} | ||
} | ||
|
||
const server = new Factory({ | ||
port: 6969 | ||
}); | ||
const client = new Jaysonic.client.http({ port: 6969 }); | ||
|
||
describe("HTTP Server Protocol Status Override", () => { | ||
before((done) => { | ||
server.listen().then(() => { | ||
done(); | ||
}); | ||
}); | ||
after((done) => { | ||
server.close().then(() => { | ||
done(); | ||
}); | ||
}); | ||
it("should overwrite status code", (done) => { | ||
client.send("foo").catch((e) => { | ||
expect(e.body.error.code).to.be.eql(-32601); | ||
expect(e.statusCode).to.be.eql(404); | ||
done(); | ||
}); | ||
}); | ||
it("should overwrite errorCode if status is passed", (done) => { | ||
class OverwritePcol extends HttpServerProtocol { | ||
clientConnected() { | ||
this.setResponseStatus({ errorCode: 400, status: 301 }); | ||
this.gotError( | ||
new Error( | ||
"{\"jsonrpc\": \"2.0\", \"error\": {\"code\": -32600, \"message\": \"Invalid Request\"}, \"id\": 2}" | ||
) | ||
); | ||
} | ||
} | ||
server.protocol = OverwritePcol; | ||
client.send("foo").catch((e) => { | ||
expect(e.body.error.code).to.be.eql(-32600); | ||
expect(e.statusCode).to.be.eql(301); | ||
done(); | ||
}); | ||
}); | ||
}); |