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

D-Pow / MockRequests
87%
master: 100%

Build:
Build:
LAST BUILD BRANCH: feature/support-origin-agnostic-slash-urls
DEFAULT BRANCH: master
Repo Added 06 Sep 2019 12:54AM UTC
Files 2
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

LAST BUILD ON BRANCH feature/support-origin-agnostic-slash-urls
branch: feature/support-origin-agnostic-slash-urls
CHANGE BRANCH
x
Reset
  • feature/support-origin-agnostic-slash-urls
  • 1.1.1
  • 1.1.11
  • 1.1.13
  • 1.1.3
  • 1.1.6
  • 1.1.7
  • 1.1.8
  • 1.1.9
  • dependabot/npm_and_yarn/demo/acorn-6.4.1
  • dependabot/npm_and_yarn/demo/elliptic-6.5.3
  • dependabot/npm_and_yarn/demo/ini-1.3.7
  • dependabot/npm_and_yarn/demo/ssri-8.0.1
  • dependabot/npm_and_yarn/demo/websocket-extensions-0.1.4
  • dependabot/npm_and_yarn/demo/y18n-4.0.1
  • dependabot/npm_and_yarn/demo/y18n-4.0.3
  • dependabot/npm_and_yarn/elliptic-6.5.3
  • dependabot/npm_and_yarn/ini-1.3.7
  • dependabot/npm_and_yarn/ini-1.3.8
  • dependabot/npm_and_yarn/y18n-4.0.1
  • feature/add-webpack-plugin
  • feature/addAutoInjectScript
  • feature/addDemo
  • feature/addNodeSupport
  • feature/addRequestApiSupport
  • master
  • v1.3.1

pending completion
250

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 %.

84 of 84 new or added lines in 1 file covered. (100.0%)

161 of 189 relevant lines covered (85.19%)

80.33 hits per line

Relevant lines Covered
Build:
Build:
189 RELEVANT LINES 161 COVERED LINES
80.33 HITS PER LINE
Source Files on feature/support-origin-agnostic-slash-urls
  • Tree
  • List 2
  • Changed 1
  • Source Changed 1
  • Coverage Changed 1
Coverage ∆ File Lines Relevant Covered Missed Hits/Line Branch Hits Branch Misses

Recent builds

Builds Branch Commit Type Ran Committer Via Coverage
250 feature/support-origin-agnostic-slash-urls 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 M... push 29 May 2021 04:53AM UTC D-Pow travis-ci pending completion  
249 feature/support-origin-agnostic-slash-urls 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 M... push 29 May 2021 04:51AM UTC D-Pow travis-ci pending completion  
See All Builds (213)
  • Repo on GitHub
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

© 2025 Coveralls, Inc