fakeHttpServer
export declare function fakeHttpServer(Class: Function, config?: IFakeServerConfig): FakeServerApi;
fakeHttpServer function creates fake instance of server api and we are using it for testing purposes.
let server: FakeServerApi;
beforeEach(() => {
server = fakeHttpServer(MyModule);
});
it("Should do GET redirect", (done) => {
server.GET("/redirect").then((api: FakeResponseApi) => {
assert.equal(api.getStatusCode(), 307);
assert.isTrue(isEqual(api.getHeaders(), {"Location": "/mypage"}));
done();
}).catch(done);
});
it("Should do GET found error", (done) => {
server.GET("/fire-error").then((api: FakeResponseApi) => {
assert.isTrue(api.getBody().toString().indexOf("ERROR=FIRE ERROR CASE") > -1);
assert.equal(api.getStatusCode(), 500);
done();
}).catch(done);
});
it("Should do GET not found", (done) => {
server.GET("/abc").then((api: FakeResponseApi) => {
assert.isTrue(api.getBody().toString().indexOf("ERROR=Router.parseRequest: /abc no route found, method: GET") > -1);
assert.equal(api.getStatusCode(), 404);
done();
}).catch(done);
});
it("Should do GET index", (done) => {
server.GET("/").then((api: FakeResponseApi) => {
assert.equal(api.getBody().toString(), "VALUE <- BEFORE");
assert.equal(api.getStatusCode(), 200);
done();
}).catch(done);
});
it("Should do OPTIONS index", (done) => {
server.OPTIONS("/").then((api: FakeResponseApi) => {
assert.equal(api.getBody().toString(), "VALUE <- BEFORE");
assert.equal(api.getStatusCode(), Status.OK);
done();
}).catch(done);
});
it("Should do CONNECT index", (done) => {
server.CONNECT("/").then((api: FakeResponseApi) => {
assert.equal(api.getBody().toString(), "VALUE <- BEFORE");
done();
}).catch(done);
});
it("Should do DELETE index", (done) => {
server.DELETE("/").then((api: FakeResponseApi) => {
assert.equal(api.getBody().toString(), "VALUE <- BEFORE");
done();
}).catch(done);
});
it("Should do HEAD index", (done) => {
server.HEAD("/").then((api: FakeResponseApi) => {
assert.equal(api.getBody().toString(), "VALUE <- BEFORE");
done();
}).catch(done);
});
it("Should do TRACE index", (done) => {
server.TRACE("/").then((api: FakeResponseApi) => {
assert.equal(api.getBody().toString(), "VALUE <- BEFORE");
done();
}).catch(done);
});
it("Should do POST index", (done) => {
server.POST("/ajax/call", Buffer.from("SENT_FROM_CLIENT")).then((api: FakeResponseApi) => {
assert.equal(api.getBody().toString(), "CALL=SENT_FROM_CLIENT");
done();
}).catch(done);
});
it("Should do PUT index", (done) => {
server.PUT("/ajax/call", Buffer.from("SENT_FROM_CLIENT")).then((api: FakeResponseApi) => {
assert.equal(api.getBody().toString(), "CALL=SENT_FROM_CLIENT");
done();
}).catch(done);
});
it("Should do PATCH index", (done) => {
server.PATCH("/ajax/call", Buffer.from("SENT_FROM_CLIENT")).then((api: FakeResponseApi) => {
assert.equal(api.getBody().toString(), "CALL=SENT_FROM_CLIENT");
done();
}).catch(done);
});