Skip to content
This repository has been archived by the owner on Jan 29, 2018. It is now read-only.

feat(main): RPC Router and Server implementation #1

Merged
merged 1 commit into from
Nov 13, 2017

Conversation

kjdelisle
Copy link
Contributor

connected to loopbackio/loopback.io#523

Currently no tests in place for RPC Server (router has most of the actual logic)

@kjdelisle kjdelisle self-assigned this Nov 13, 2017
@kjdelisle kjdelisle force-pushed the initial-implementation branch from 411a53b to 228fe56 Compare November 13, 2017 21:24
@kjdelisle kjdelisle merged commit f7f5df8 into master Nov 13, 2017
@kjdelisle kjdelisle deleted the initial-implementation branch November 13, 2017 21:47
expect(controller.basicHello({})).to.equal('Hello, World!');
});

it('returns greetings for the world without valid input', () => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it('returns greetings with valid input, ...

it('routes to existing controller and method', async () => {
await router.routeRequest(request, response);
const stub = response.send as sinon.SinonStub;
expect(stub.called);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you seen this assertion to fail? I believe it's a no-op:

> expect(false)
Assertion {
  obj: false,
  anyOne: false,
  negate: false,
  params: { actual: false } }

I think you wanted to say expect(stub.called).to.be.true() but please don't do that - the assertion failure will be rather unhelpful. Use sinon assertions instead:

sinon.assert.called(stub);

We tried to integrate should with sinon via https://github.com/shouldjs/sinon, but it did not work :(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix other places using expect(stub.called); too


function getRequest(req?: Partial<express.Request>) {
const reqt = Object.assign(
<express.Request>{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh oh, this is hacky and may easily break when express starts to require on another request property that's not mocked out here, or calls any of the ReadableStream APIs. Have you considered using Shot instead? We are shipping it as a part of our testlab, see packages/testlab/src/shot.ts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bajtos Would you be able to provide an example of how to use our testlab exports of Shot? I've noticed that we're not actually using it the way Shot's Github README advertises, and I'm not certain how I would use it to mock the handling of express requests and responses (given that it's for http.ServerRequest and http.ServerResponse).

I understand that the hard cast here could break if something leverages a function or property that I haven't faked, but I've gone out of my way to make sure that nothing calls functions on the request or response object in the rpc-router code above and beyond what's faked here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically speaking, no instance of an express server ever begins listening on an external port; these tests are essentially integration tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be able to provide an example of how to use our testlab exports of Shot?

Have you tried to search for the places in loopback-next that are using it? There are quire few test files leveraging mockResponse, see https://github.com/strongloop/loopback-next/search?utf8=%E2%9C%93&q=shot&type=

Looks like we need to improve the docs for this part of testlab! I created an issue - see loopbackio/loopback-next#760

}

function getResponse(res?: Partial<express.Response>) {
const resp = <express.Response>{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto here, please consider using Shot's Response mock instead.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants