Skip to content

@warp-drive/holodeck Broken mock lookup for requests with searchParams #10504

Description

@dagroe

Using "@warp-drive/holodeck": "0.0.4" when registering a mock that contains searchParams, e.g. /test?param=1, the requests to that URL result in MOCK_NOT_FOUND.

Looking at the node server implementation, I see that when registering the mock, the cache key is built with the url defined in the payload:

const { url, headers, method, status, statusText, body, response } = payload;
        const cacheKey = generateFilepath({
          projectRoot,
          testId,
          url,
          method,
          body,
          testRequestNumber,
        });

Later, when checking actual requests, the cache key is built using niceUrl:

const cacheKey = generateFilepath({
          projectRoot,
          testId,
          url: niceUrl,
          method: req.method,
          body: body ? body : null,
          testRequestNumber,
        });

The problem is that getNiceUrl strips the ? between pathname and searchParams, so we get two different cache keys for register and retrieval.

I was able to fix this by keeping the ? when relevant, e.g.

export function getNiceUrl(url) {
  const urlObj = new URL(url);
  urlObj.searchParams.delete('__xTestId');
  urlObj.searchParams.delete('__xTestRequestNumber');
  let params = urlObj.searchParams.toString();
  if (params) {
    params = '?' + params;
  }
  return (urlObj.pathname + params).slice(1);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions