• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

D-Pow / MockRequests / 249
87%
master: 100%

Build:
Build:
LAST BUILD BRANCH: feature/support-origin-agnostic-slash-urls
DEFAULT BRANCH: master
Ran 29 May 2021 04:51AM UTC
Jobs 1
Files 2
Run time 1s
Badge
Embed ▾
README BADGES
x

If you need to use a raster PNG badge, change the '.svg' to '.png' in the link

Markdown

Textile

RDoc

HTML

Rst

pending completion
249

push

travis-ci

D-Pow
Refactor code to allow 'slash URLs' to work with any origin

"Slash URLs" are URLs that begin with '/' instead of the domain/origin (e.g. `/myApi` instead of `https://website.com/myApi`). When using `fetch`/`XHR` directly, this poses no issue to MockRequests. But when using some third-party libraries, they automatically prepend a user-specified domain to the URL which breaks the `urlIsMocked()` check.

For example, if a user wrote a wrapper function around Axios where the parameter was just the API path without the domain, you'd end up with:

```javascript
function doGet(url = '/myApi') {
    axios.get(
        url,
        {
            baseUrl: 'https://website.com'
        }
    );
}

// will call `fetch('https://website.com/myApi')` instead of `fetch('/myApi')`
```

In order to fix this, the URL passed to `fetch`/`XHR` needs to be split up more than just the pathname and query parameters. If the user mocked `/myApi`, we need to be able to check if the pathname is mocked regardless of the domain/origin. This is especially the case when users have testing environments (which is the primary motivation for writing wrapper functions around e.g. Axios).

Thus, we do these primary changes:

* Split up the URL into more segments in `getPathnameAndQueryParams()` for more in-depth parsing.
* Normalize the URLs to remove ending slashes (to avoid e.g. `mock('/myApi')` not working for `fetch('/myApi/')`).
    - Involves changing what is stored in the `urlResponseMap`.
* Iterate through all possible URL combinations to find which combo was actually stored in the `urlResponseMap`, and then check if the URL from `fetch` matches them accordingly.
    - Do so in new `getMockedUrlFromUrlArg()` function.
    - This also does an additional `location.origin` check to see if the mocked URL was created from the actual origin vs another origin.
        + **TODO** This is likely unnecessary; consider removing it.
        + **TODO** Add tests for this if it is kept.
    - *... (continued)

83 of 90 branches covered (92.22%)

Branch coverage included in aggregate %.

161 of 189 relevant lines covered (85.19%)

80.33 hits per line

Jobs
ID Job ID Ran Files Coverage
1 249.1 29 May 2021 04:51AM UTC 0
87.46
Travis Job 249.1
Source Files on build 249
Detailed source file information is not available for this build.
  • Back to Repo
  • Travis Build #249
  • f03f6ad2 on github
  • Next Build on feature/support-origin-agnostic-slash-urls (#250)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc