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

D-Pow / MockRequests / 250 / 1
87%
master: 100%

Build:
Build:
LAST BUILD BRANCH: feature/support-origin-agnostic-slash-urls
DEFAULT BRANCH: master
Ran 29 May 2021 04:53AM UTC
Files 2
Run time 0s
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

29 May 2021 04:52AM UTC coverage: 87.455%. Remained the same
250.1

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

Source Files on job 250.1
  • Tree
  • List 0
  • Changed 1
  • Source Changed 1
  • Coverage Changed 1
Coverage ∆ File Lines Relevant Covered Missed Hits/Line Branch Hits Branch Misses
  • Back to Build 210
  • Travis Job 250.1
  • 733d8427 on github
  • Prev Job for on feature/support-origin-agnostic-slash-urls (#249.1)
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