Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: add integration tests #402

Closed
josephjclark opened this issue Oct 6, 2023 · 0 comments
Closed

http: add integration tests #402

josephjclark opened this issue Oct 6, 2023 · 0 comments

Comments

@josephjclark
Copy link
Collaborator

josephjclark commented Oct 6, 2023

We should add a suite of integration tests in the new http adaptor.

This creates a server and uses the http functions to call it.

As well as some very basic get/put/post stuff, it can test redirects, form data, auth and tls security - stuff that's harder to unit test with the undici mocks

--

Here's a unit test for http which uses redirects:

 it('can follow redirects', async () => {
    // mock intercept paths
    // note that the mock undici client won't respect the redirect
    testServer
      .intercept({
        path: '/api/fake-endpoint',
        method: 'GET',
      })
      .reply(
        301,
        1,
        {
          headers: {
            Location: 'https://www.example.com/api/fake-endpoint-2',
          },
        }
      );

    testServer
      .intercept({
        path: '/api/fake-endpoint-2',
        method: 'GET',
      })
      .reply(302, 2, {
        headers: {
          Location: 'https://www.example.com/api/fake-endpoint-3',
        },
      });

    testServer.intercept(
      {
        path: '/api/fake-endpoint-3',
        method: 'GET',
      }).reply(200, res => res.path)
    );

    const state = {
      configuration: {},
      data: {},
    };

    const finalState = await execute(
      get('https://www.example.com/api/fake-endpoint', {
        followAllRedirects: true,
      })
    )(state);

    expect(finalState.data.url).to.eql('/api/fake-endpoint-3');
  });

This test doesn't work against the undici mock interceptor because the redirect flag is not respected.

To get this test to pass, we should:

  • create a lightweight http server in test
  • add redirect routes to the server
  • call the actual server
  • test the result
  • tear down the server

It's kind of a big test and not worth the time right now.

Here's a koa server we can use:

import Koa from "koa";

const server = new Koa();

server.use(async (ctx) => {
  if (ctx.url === "/redirect") {
    ctx.response.status = 301;
    ctx.response.set({ Location: "http://localhost:2222/foo" });
  } else {
    ctx.response.status = 200;
    ctx.response.body = "ok";
  }
});

server.listen(2222);
@josephjclark josephjclark changed the title http: add unit test for redirect http: add integration tests Oct 10, 2023
@mtuchi mtuchi mentioned this issue Oct 19, 2023
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant