我目前正在尝试编写一些需要response.request.responseURL
在 axios 处理响应时设置值的功能。但是,我似乎无法使用 moxios 设置此值。这是我的测试的样子:
it('resets list selection if input contents were replaced', (done) => {
component.currently_selected_list_value = 10;
component.last_called_url = 'henkiehoutman';
let input = domElt.querySelector('input');
ReactTestUtils.Simulate.change(input);
moxios.wait(() => {
let request = moxios.requests.mostRecent();
request.respondWith({
status: 200,
response: [{
customer_info_customers: ['', '', '', '', ''],
domain_info_customers: {},
}],
request: {responseURL: 'banaan'}
}).then(() => {
// The contents were replaced, so it's best to forget whatever our selection was.
expect(component.currently_selected_list_value).toEqual(-1);
done();
});
});
});
这是我的实际应用程序的样子:
onChange (event) {
return axios.get(
this.props.ApiUrl + encodeURIComponent(event.target.value)
).then(
(response) => {
let response_url = response.request.responseURL; // This is the value I want to set manually.
if(this.shouldHandleResponse(response_url)){
this.last_called_url = response_url;
let data = response.data;
this.setState({results: data, dropdown: data.length > 0});
if ( this.currently_selected_list_value > max_index) {
this.currently_selected_list_value = max_index;
}
}
}
);
},
但是,当我记录这个值是什么时,它只是说undefined
. 这就是实际response.request
值(将其放在引号中,以便更具可读性):
日志:请求{解决:函数(a){...},拒绝:函数(a){...},配置:对象{适配器:函数模拟适配器(配置){...},transformRequest:对象{0 : ...}, transformResponse: Object{0: ...}, 超时: 0, xsrfCookieName: 'XSRF-TOKEN', xsrfHeaderName: 'X-XSRF-TOKEN', maxContentLength: -1, validateStatus: 函数 validateStatus(status ) { ... },标题:Object{Accept:...,X-CSRFToken:...},方法:'get',url:'my_api_url/',数据:未定义},标题:Object{Accept: 'application/json, text/plain, / ', X-CSRFToken: 'my_csrf_token'}, url: 'my_api_url/', 超时: 0, withCredentials: false, responseType: undefined}
这会破坏我的应用程序,因为responseURL
应该始终定义 a。看起来它正在覆盖我在 moxios 中定义的请求。这不一定是坏事,因为我认为它需要这些东西才能正常运行。但是,如果我可以为这个请求添加一些值,那就太好了。
所以; 如何responseURL
在 moxios 中为此请求添加值?