Skip to content

Commit

Permalink
fix + improve axios tests (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
joemcelroy authored and ssetem committed Mar 8, 2017
1 parent ecb5d99 commit e22647f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
44 changes: 43 additions & 1 deletion src/__test__/core/transport/AxiosESTransportSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("AxiosESTransport", ()=> {
"Content-Type":"application/json"
})
expect(transport.axios.defaults.auth.username).toBe("key")
expect(transport.axios.defaults.auth.password).toBe("value")
expect(transport.axios.defaults.auth.password).toBe("val")
expect(transport.options.timeout).toEqual(10000)
expect(transport.options.searchUrlPath).toBe("/_search/")
})
Expand All @@ -64,7 +64,49 @@ describe("AxiosESTransport", ()=> {
})
})

it("search - basicAuth", (done)=> {
let mockResults = {hits:[1,2,3]}
this.host = "http://search:9200/"
this.transport = new AxiosESTransport(this.host, {
searchUrlPath:"/search",
basicAuth: 'user:pass'
})
jasmine.Ajax.stubRequest(this.host + "search").andReturn({
"responseText": JSON.stringify(mockResults)
});
this.transport.search({
size:10,
from:0
}).then((result)=> {
expect(result.hits).toEqual([1,2,3])
let request = jasmine.Ajax.requests.mostRecent()
expect(request.requestHeaders['Authorization'])
.toEqual("Basic " + btoa("user:pass"))
done()
})
})

it("search - withCredentials", (done)=> {
document.cookie = axios.defaults.xsrfCookieName + '=12345';
let mockResults = {hits:[1,2,3]}
this.host = "http://search:9200/"
this.transport = new AxiosESTransport(this.host, {
searchUrlPath:"/search",
withCredentials: true
})
jasmine.Ajax.stubRequest(this.host + "search").andReturn({
"responseText": JSON.stringify(mockResults)
});
this.transport.search({
size:10,
from:0
}).then((result)=> {
expect(result.hits).toEqual([1,2,3])
let request = jasmine.Ajax.requests.mostRecent()
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual('12345');
done()
})
})

it("test timeout", ()=> {
AxiosESTransport.timeout = 10
Expand Down
2 changes: 1 addition & 1 deletion src/core/transport/AxiosESTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class AxiosESTransport extends ESTransport{
let credentials = {}
if (options.basicAuth !== undefined) {
const parsed = options.basicAuth.split(":")
const auth = { username: parsed[0], parsed: credentials[1] }
const auth = { username: parsed[0], password: parsed[1] }
credentials['auth'] = auth
}
if (options.withCredentials !== undefined) {
Expand Down

0 comments on commit e22647f

Please sign in to comment.