I'm trying to write a mock test for the spotify web api authorize endpoint. I have the following code for nock:
nock("https://accounts.spotify.com/")
.post("/api/token", {
grant_type: "authorization_code",
code: "DummyAccessCode",
redirect_uri: "http://localhost:3000"
})
.reply(201, {access_token: 12345, refresh_token: 67890, expires_in: 3600});
My request in order to get a mocked request is:
const res = await request("https://accounts.spotify.com")
.post("/api/token")
.send({
grant_type: "authorization_code",
code: "DummyAccessCode",
redirect_uri: "http://localhost:3000"
})
I keep getting the error in the title once it hits the request and I don't know why. The request should match the nock but it isn't. Another detail to note is that when I run console.log(nock.activeMocks());
it prints: 'POST https://accounts.spotify.com:443/api/token'
Which doesn't make sense to me because I never placed a 443 in the url. Really hoping someone can help me with this because I've been struggling with writing my tests for the past 2 weeks and finally made it to this stage of understanding. Thank you all in advance.
To everyone I solved the error. It was just an issue with my URL for the nock that I passed in. It was missing a '/'.
Here's the code for future reference for anyone wondering:
it("/login should return access token, refresh token, and expiriation", async () => {
nock("https://accounts.spotify.com/", {
reqheaders: {
"content-type": "application/json",
}
})
.post("/api/token", {
grant_type: "authorization_code",
code: "DummyAccessCode",
redirect_uri: "http://localhost:3000"
})
.reply(201, {access_token: 12345, refresh_token: 67890, expires_in: 3600});
const res = await request("https://accounts.spotify.com")
.post("/api/token")
.send({
grant_type: "authorization_code",
code: "DummyAccessCode",
redirect_uri: "http://localhost:3000"
})
.expect(201);