The only way by now is accessing request headers in .reply and validate it here:

mockAxios.onPut(`xxxxx.com`).reply((config) => {
  expect(config.headers."Content-Type").toEqual("What do you expect here");
  return [200, expectedResult, {
    Authorization: `Bearer ${token}`,
    "Content-type": "application/x-www-form-urlencoded",
  }];
});

Actually I believe it should also be possible in declarative way:

mockAxios.onPut(`xxxxx.com`, undefined, { 
  expectedHeader1: "value1", 
  expectedHeader2: "value2"}
).reply(200, expectedResult);

So it would just throw instead of returning mock response if request headers did not match.

But it does no work this way by now.

Reason: axios-mock-adapter uses deepEqual for such a filtering. So we would need specify there not just few required headers(we are focusing on) but all headers including those axios adds on its own(like Accept). So it is not really readable.

I've filed #219 in their repo on this. If it was not intentional for any reason, that may be fixed in future.

Answer from skyboyer on Stack Overflow
🌐
npm
npmjs.com › package › axios-mock-adapter
axios-mock-adapter - npm
October 9, 2024 - mock.onGet("/users").reply(function (config) { // `config` is the axios config and contains things like the url // return an array in the form of [status, data, headers] return [ 200, { users: [{ id: 1, name: "John Smith" }], }, ]; });
      » npm install axios-mock-adapter
    
Published   Oct 09, 2024
Version   2.1.0
Author   Colin Timmermans
🌐
GitHub
github.com › ctimmerm › axios-mock-adapter
GitHub - ctimmerm/axios-mock-adapter: Axios adapter that allows to easily mock requests
mock.onGet("/users").reply(function (config) { // `config` is the axios config and contains things like the url // return an array in the form of [status, data, headers] return [ 200, { users: [{ id: 1, name: "John Smith" }], }, ]; });
Starred by 3.5K users
Forked by 255 users
Languages   JavaScript 96.2% | TypeScript 3.8% | JavaScript 96.2% | TypeScript 3.8%
🌐
GitHub
github.com › ctimmerm › axios-mock-adapter › issues › 379
Support response header · Issue #379 · ctimmerm/axios-mock-adapter
October 26, 2023 - In order to mock auth route I need to provide a custom header in the reponse that's returned by the server. As far as I can tell, only body of the response can be specified neither documentatio...
Author   Caellian
🌐
GitLab
docs.gitlab.com › ee › development › fe_guide › axios.html
Axios | GitLab Docs
import axios from './lib/utils/axios_utils'; axios.get(url) .then((response) => { // `data` is the response that was provided by the server const data = response.data; // `headers` the headers that the server responded with // All header names are lower cased const paginationData = response.headers; }) .catch(() => { //handle the error }); To help us mock the responses we are using axios-mock-adapter.
🌐
PRACE
repository.prace-ri.eu › help
Axios · Fe guide · Development · Help · GitLab
import axios from './lib/utils/axios_utils'; axios.get(url) .then((response) => { // `data` is the response that was provided by the server const data = response.data; // `headers` the headers that the server responded with // All header names are lower cased const paginationData = response.headers; }) .catch(() => { //handle the error }); To help us mock the responses we are using axios-mock-adapter.
🌐
UNPKG
app.unpkg.com › axios-mock-adapter@1.21.1 › files › README.md
axios-mock-adapter
## Example Mocking a `GET` request ```js var axios = require("axios"); var MockAdapter = require("axios-mock-adapter"); // This sets the mock adapter on the default instance var mock = new MockAdapter(axios); // Mock any GET request to /users // arguments for reply are (status, data, headers) mock.onGet("/users").reply(200, { users: [{ id: 1, name: "John Smith" }], }); axios.get("/users").then(function (response) { console.log(response.data); }); ``` Mocking a `GET` request with specific parameters ```js var axios = require("axios"); var MockAdapter = require("axios-mock-adapter"); // This set
🌐
Medium
simonkkaranja.medium.com › react-testing-mocking-axios-with-axios-mock-adapter-e24752a55923
React Testing: Mocking Axios with axios-mock-adapter | by Simon Karanja | Medium
December 14, 2020 - The reply() takes the following arguments:- (status, data, headers) in that order. If you run the tests i.e. ... The test should pass. ... This will return a failed promise with Error(‘Network Error’). If say we wanted a network timeout, we could have done:- ... This would have returned a failed promise with Error code set to ECONNABORTED. Now running the tests again, they should all be in green. ... That’s how easy it is to mock axios requests with axios-mock-adapter...
🌐
GitHub
github.com › ctimmerm › axios-mock-adapter › pull › 130
Support matching query params for `post`, `put` methods. by thk2b · Pull Request #130 · ctimmerm/axios-mock-adapter
May 30, 2019 - it('can pass a body, query params, and headers to match a handler', function() { var headers = { Accept: 'application/json, text/plain, */*', 'Content-Type': 'application/x-www-form-urlencoded', 'Header-test': 'test-header' }; mock.onPost('/withBodyAndParams', { data: { is: 'passed' }, in: true }, headers, { foo: 'bar', bar: 'baz' }).reply(200); return instance .post('/withBodyAndParams', { data: { is: 'passed' }, in: true }, { headers: headers, params: { foo: 'bar', bar: 'baz' }}) .then(function(response) { expect(response.status).to.equal(200); }); });
Author   ctimmerm
Find elsewhere
🌐
GitHub
github.com › ctimmerm › axios-mock-adapter › releases
Releases · ctimmerm/axios-mock-adapter
October 9, 2024 - Change the parameters of the methods to align it to the one of axios. (#387) The last parameter must be a config object with {params, headers} instead of just the headers.
Author   ctimmerm
🌐
TechNetExperts
technetexperts.com › home › easily mock requests with axios mock adapter
How to Use Axios Mock Adapter to Test Network Requests
November 13, 2025 - Every API Response has structure, like status, data, etc. Same way AXIOS Mock Adapter contains Response Structure. Which contains, status, data, headers
🌐
Nicedoc
nicedoc.io › ctimmerm › axios-mock-adapter
Redirecting...
June 2, 2022 - We cannot provide a description for this page right now
🌐
Tabnine
tabnine.com › home page › code › javascript › mockadapter
axios-mock-adapter.MockAdapter.onPost JavaScript and Node.js code examples | Tabnine
axiosMock.onPost(urls.userList()).reply(201, user) const store = mockStore(StoreFactory.build()) store.dispatch(createUser(user.username, user.firstName, user.lastName)) ... .onPost(`repositories/${repositoryId}/like`) .reply(200, { id: repositoryId, .onPost(`repositories/${repositoryId}/like`) .reply(200, { id: repositoryId, ... expect.assertions(1) const api = new Kitsu({ headers: { Authorization: true } }) mock.onPost('/anime').reply(config => { expect(config.headers).toEqual({ Accept: 'application/vnd.api+json', expect.assertions(1) const api = new Kitsu({ headers: { Authorization: true }
🌐
GitHub
github.com › dazralsky › axios-mock-adapter
GitHub - dazralsky/axios-mock-adapter: Axios adapter that allows to easily mock requests
June 2, 2022 - mock.onGet("/users").reply(function (config) { // `config` is the axios config and contains things like the url // return an array in the form of [status, data, headers] return [ 200, { users: [{ id: 1, name: "John Smith" }], }, ]; });
Author   dazralsky