How to expect httpReq.request.headers.get('authorization').toBeTruthy()?

Multi tool use
Multi tool use












-1















I am having a problem to get the unit testing of interceptor done because it tells me that httpReq.request.headers.get('authorization').toBeTruthy() must be tobefalsy().even i am giving the token to it and then attaching that token with the authorization Token Header as shown in the following code but this gives me error. This is interceptor service code:



`



export class TokenInterceptorService implements HttpInterceptor {
constructor(private auth: CoreService) {
}
intercept(req: HttpRequest<any>, next: HttpHandler) {
if (this.auth.getToken()) {
req = req.clone({
setHeaders: {
authorization: `Token ${this.auth.getToken()}`
}
});
}
return next.handle(req);
}
}


`



i have tried different approaches to make this correct and even i have debugged this code token is coming but when we expect this to be truthy this tells me that token is null



this is code of spec file



`



 it('should add a Authorization token to the authorization header', () => {
coreService.setToken('this is my dummy token');
const token = coreService.getToken();
const spy = coreSpy.getToken();
const someData = { data: 'someData ' };
http.get('localhost:3000/anonymous/user').subscribe((data) => {
expect(data).toEqual(someData);
});
const httpReq = httpTestingController.expectOne('localhost:3000/anonymous/user');
expect(httpReq.request.method).toBe('GET');
expect(httpReq.request.headers.get('authorization')).toBeTruthy();
expect(httpReq.request.headers.get('authorization')).toBe(`Token ${token}`);
httpReq.flush(someData);
httpTestingController.verify();
});


`



expected result is that it must tell me that this is true that we get authorization.










share|improve this question























  • What does your spied getToken return? Because the header is only set if it's truthy.

    – jonrsharpe
    Jan 3 at 12:42











  • getToken() returns the token of this my dummy token.

    – Asad Fiaz
    Jan 3 at 12:49











  • getToken() is giving the result but its not being attached with the headers

    – Asad Fiaz
    Jan 3 at 12:49











  • setToken(token: string) { localStorage.setItem(this.storageKey, token); }

    – Asad Fiaz
    Jan 3 at 12:50











  • getToken() { return localStorage.getItem(this.storageKey); }

    – Asad Fiaz
    Jan 3 at 12:50
















-1















I am having a problem to get the unit testing of interceptor done because it tells me that httpReq.request.headers.get('authorization').toBeTruthy() must be tobefalsy().even i am giving the token to it and then attaching that token with the authorization Token Header as shown in the following code but this gives me error. This is interceptor service code:



`



export class TokenInterceptorService implements HttpInterceptor {
constructor(private auth: CoreService) {
}
intercept(req: HttpRequest<any>, next: HttpHandler) {
if (this.auth.getToken()) {
req = req.clone({
setHeaders: {
authorization: `Token ${this.auth.getToken()}`
}
});
}
return next.handle(req);
}
}


`



i have tried different approaches to make this correct and even i have debugged this code token is coming but when we expect this to be truthy this tells me that token is null



this is code of spec file



`



 it('should add a Authorization token to the authorization header', () => {
coreService.setToken('this is my dummy token');
const token = coreService.getToken();
const spy = coreSpy.getToken();
const someData = { data: 'someData ' };
http.get('localhost:3000/anonymous/user').subscribe((data) => {
expect(data).toEqual(someData);
});
const httpReq = httpTestingController.expectOne('localhost:3000/anonymous/user');
expect(httpReq.request.method).toBe('GET');
expect(httpReq.request.headers.get('authorization')).toBeTruthy();
expect(httpReq.request.headers.get('authorization')).toBe(`Token ${token}`);
httpReq.flush(someData);
httpTestingController.verify();
});


`



expected result is that it must tell me that this is true that we get authorization.










share|improve this question























  • What does your spied getToken return? Because the header is only set if it's truthy.

    – jonrsharpe
    Jan 3 at 12:42











  • getToken() returns the token of this my dummy token.

    – Asad Fiaz
    Jan 3 at 12:49











  • getToken() is giving the result but its not being attached with the headers

    – Asad Fiaz
    Jan 3 at 12:49











  • setToken(token: string) { localStorage.setItem(this.storageKey, token); }

    – Asad Fiaz
    Jan 3 at 12:50











  • getToken() { return localStorage.getItem(this.storageKey); }

    – Asad Fiaz
    Jan 3 at 12:50














-1












-1








-1








I am having a problem to get the unit testing of interceptor done because it tells me that httpReq.request.headers.get('authorization').toBeTruthy() must be tobefalsy().even i am giving the token to it and then attaching that token with the authorization Token Header as shown in the following code but this gives me error. This is interceptor service code:



`



export class TokenInterceptorService implements HttpInterceptor {
constructor(private auth: CoreService) {
}
intercept(req: HttpRequest<any>, next: HttpHandler) {
if (this.auth.getToken()) {
req = req.clone({
setHeaders: {
authorization: `Token ${this.auth.getToken()}`
}
});
}
return next.handle(req);
}
}


`



i have tried different approaches to make this correct and even i have debugged this code token is coming but when we expect this to be truthy this tells me that token is null



this is code of spec file



`



 it('should add a Authorization token to the authorization header', () => {
coreService.setToken('this is my dummy token');
const token = coreService.getToken();
const spy = coreSpy.getToken();
const someData = { data: 'someData ' };
http.get('localhost:3000/anonymous/user').subscribe((data) => {
expect(data).toEqual(someData);
});
const httpReq = httpTestingController.expectOne('localhost:3000/anonymous/user');
expect(httpReq.request.method).toBe('GET');
expect(httpReq.request.headers.get('authorization')).toBeTruthy();
expect(httpReq.request.headers.get('authorization')).toBe(`Token ${token}`);
httpReq.flush(someData);
httpTestingController.verify();
});


`



expected result is that it must tell me that this is true that we get authorization.










share|improve this question














I am having a problem to get the unit testing of interceptor done because it tells me that httpReq.request.headers.get('authorization').toBeTruthy() must be tobefalsy().even i am giving the token to it and then attaching that token with the authorization Token Header as shown in the following code but this gives me error. This is interceptor service code:



`



export class TokenInterceptorService implements HttpInterceptor {
constructor(private auth: CoreService) {
}
intercept(req: HttpRequest<any>, next: HttpHandler) {
if (this.auth.getToken()) {
req = req.clone({
setHeaders: {
authorization: `Token ${this.auth.getToken()}`
}
});
}
return next.handle(req);
}
}


`



i have tried different approaches to make this correct and even i have debugged this code token is coming but when we expect this to be truthy this tells me that token is null



this is code of spec file



`



 it('should add a Authorization token to the authorization header', () => {
coreService.setToken('this is my dummy token');
const token = coreService.getToken();
const spy = coreSpy.getToken();
const someData = { data: 'someData ' };
http.get('localhost:3000/anonymous/user').subscribe((data) => {
expect(data).toEqual(someData);
});
const httpReq = httpTestingController.expectOne('localhost:3000/anonymous/user');
expect(httpReq.request.method).toBe('GET');
expect(httpReq.request.headers.get('authorization')).toBeTruthy();
expect(httpReq.request.headers.get('authorization')).toBe(`Token ${token}`);
httpReq.flush(someData);
httpTestingController.verify();
});


`



expected result is that it must tell me that this is true that we get authorization.







angular unit-testing angular6 angular-services angular-http-interceptors






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 9:15









Asad FiazAsad Fiaz

13




13













  • What does your spied getToken return? Because the header is only set if it's truthy.

    – jonrsharpe
    Jan 3 at 12:42











  • getToken() returns the token of this my dummy token.

    – Asad Fiaz
    Jan 3 at 12:49











  • getToken() is giving the result but its not being attached with the headers

    – Asad Fiaz
    Jan 3 at 12:49











  • setToken(token: string) { localStorage.setItem(this.storageKey, token); }

    – Asad Fiaz
    Jan 3 at 12:50











  • getToken() { return localStorage.getItem(this.storageKey); }

    – Asad Fiaz
    Jan 3 at 12:50



















  • What does your spied getToken return? Because the header is only set if it's truthy.

    – jonrsharpe
    Jan 3 at 12:42











  • getToken() returns the token of this my dummy token.

    – Asad Fiaz
    Jan 3 at 12:49











  • getToken() is giving the result but its not being attached with the headers

    – Asad Fiaz
    Jan 3 at 12:49











  • setToken(token: string) { localStorage.setItem(this.storageKey, token); }

    – Asad Fiaz
    Jan 3 at 12:50











  • getToken() { return localStorage.getItem(this.storageKey); }

    – Asad Fiaz
    Jan 3 at 12:50

















What does your spied getToken return? Because the header is only set if it's truthy.

– jonrsharpe
Jan 3 at 12:42





What does your spied getToken return? Because the header is only set if it's truthy.

– jonrsharpe
Jan 3 at 12:42













getToken() returns the token of this my dummy token.

– Asad Fiaz
Jan 3 at 12:49





getToken() returns the token of this my dummy token.

– Asad Fiaz
Jan 3 at 12:49













getToken() is giving the result but its not being attached with the headers

– Asad Fiaz
Jan 3 at 12:49





getToken() is giving the result but its not being attached with the headers

– Asad Fiaz
Jan 3 at 12:49













setToken(token: string) { localStorage.setItem(this.storageKey, token); }

– Asad Fiaz
Jan 3 at 12:50





setToken(token: string) { localStorage.setItem(this.storageKey, token); }

– Asad Fiaz
Jan 3 at 12:50













getToken() { return localStorage.getItem(this.storageKey); }

– Asad Fiaz
Jan 3 at 12:50





getToken() { return localStorage.getItem(this.storageKey); }

– Asad Fiaz
Jan 3 at 12:50












1 Answer
1






active

oldest

votes


















-1














I think we ran into the same issue and in the end we decided to only unit test the request and verify, that req.clone is called with the right params using a spy rather than testing, that clone uses the param to alter the request accordingly.






share|improve this answer
























  • Erbsenkoenig can you share the code? How did you do that?

    – Asad Fiaz
    Jan 3 at 12:47











  • This is a test of implementation, not behaviour.

    – jonrsharpe
    Jan 3 at 12:53











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019269%2fhow-to-expect-httpreq-request-headers-getauthorization-tobetruthy%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














I think we ran into the same issue and in the end we decided to only unit test the request and verify, that req.clone is called with the right params using a spy rather than testing, that clone uses the param to alter the request accordingly.






share|improve this answer
























  • Erbsenkoenig can you share the code? How did you do that?

    – Asad Fiaz
    Jan 3 at 12:47











  • This is a test of implementation, not behaviour.

    – jonrsharpe
    Jan 3 at 12:53
















-1














I think we ran into the same issue and in the end we decided to only unit test the request and verify, that req.clone is called with the right params using a spy rather than testing, that clone uses the param to alter the request accordingly.






share|improve this answer
























  • Erbsenkoenig can you share the code? How did you do that?

    – Asad Fiaz
    Jan 3 at 12:47











  • This is a test of implementation, not behaviour.

    – jonrsharpe
    Jan 3 at 12:53














-1












-1








-1







I think we ran into the same issue and in the end we decided to only unit test the request and verify, that req.clone is called with the right params using a spy rather than testing, that clone uses the param to alter the request accordingly.






share|improve this answer













I think we ran into the same issue and in the end we decided to only unit test the request and verify, that req.clone is called with the right params using a spy rather than testing, that clone uses the param to alter the request accordingly.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 12:01









ErbsenkoenigErbsenkoenig

1705




1705













  • Erbsenkoenig can you share the code? How did you do that?

    – Asad Fiaz
    Jan 3 at 12:47











  • This is a test of implementation, not behaviour.

    – jonrsharpe
    Jan 3 at 12:53



















  • Erbsenkoenig can you share the code? How did you do that?

    – Asad Fiaz
    Jan 3 at 12:47











  • This is a test of implementation, not behaviour.

    – jonrsharpe
    Jan 3 at 12:53

















Erbsenkoenig can you share the code? How did you do that?

– Asad Fiaz
Jan 3 at 12:47





Erbsenkoenig can you share the code? How did you do that?

– Asad Fiaz
Jan 3 at 12:47













This is a test of implementation, not behaviour.

– jonrsharpe
Jan 3 at 12:53





This is a test of implementation, not behaviour.

– jonrsharpe
Jan 3 at 12:53




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019269%2fhow-to-expect-httpreq-request-headers-getauthorization-tobetruthy%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







b,9NHyYRVGyU
rv6lG,XNTOb C3 m5 AnsSv cZQLnrOgvnDZBUdJRt5PonpwlUV b

Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas