Craig
Craig7d ago

Is there a way to test a request's configuration?

Deno.test("authenticate() uses the expected request", async () => {

// arrange
const mockFetch = new MockFetch();
mockFetch
.intercept('https://authentication.xxx.com/connect/token', { method: "POST" })
.response(JSON.stringify({
access_token: crypto.randomUUID(),
expires_in: 604800
}), { status: 200 });

const client_id = crypto.randomUUID();
const client_secret = crypto.randomUUID();

const body = {
grant_type: 'password',
client_id: client_id,
client_secret: client_secret,
}
const formBody = Object.keys(body)
.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(body[key])).join('&');

const client = new Client(client_id,client_secret);

// act
await client.authenticate();

// assert
assertEquals(mockFetch.request.url, 'https://authentication.xxx.com/connect/token');
assertEquals(mockFetch.request.method, 'POST');
assertEquals(mockFetch.request.headers('Content-Type'), 'application/x-www-form-urlencoded');
assertEquals(mockFetch.request.body, formBody);

});
Deno.test("authenticate() uses the expected request", async () => {

// arrange
const mockFetch = new MockFetch();
mockFetch
.intercept('https://authentication.xxx.com/connect/token', { method: "POST" })
.response(JSON.stringify({
access_token: crypto.randomUUID(),
expires_in: 604800
}), { status: 200 });

const client_id = crypto.randomUUID();
const client_secret = crypto.randomUUID();

const body = {
grant_type: 'password',
client_id: client_id,
client_secret: client_secret,
}
const formBody = Object.keys(body)
.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(body[key])).join('&');

const client = new Client(client_id,client_secret);

// act
await client.authenticate();

// assert
assertEquals(mockFetch.request.url, 'https://authentication.xxx.com/connect/token');
assertEquals(mockFetch.request.method, 'POST');
assertEquals(mockFetch.request.headers('Content-Type'), 'application/x-www-form-urlencoded');
assertEquals(mockFetch.request.body, formBody);

});
0 Replies
No replies yetBe the first to reply to this messageJoin