Skip to content

Commit

Permalink
Replace superagent with fetch library wide
Browse files Browse the repository at this point in the history
This commit also does the following.

- All responses return a `Headers` object instead of a plain object,
  matching the behavior of the underlying `fetch` calls.
- Updates all test mocking to use `jest-fetch-mock`.
- Cleans up unneeded development dependencies.
  • Loading branch information
eligundry committed Jul 1, 2023
1 parent be15f1c commit cee94fd
Show file tree
Hide file tree
Showing 15 changed files with 11,249 additions and 4,309 deletions.
81 changes: 0 additions & 81 deletions __mocks__/superagent.js

This file was deleted.

18 changes: 9 additions & 9 deletions __tests__/authentication-request.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
var AuthenticationRequest = require('../src/authentication-request');
var AuthenticationRequest = require("../src/authentication-request");

describe('Create Authentication Requests', () => {
test('Should use default settings if none are supplied', () => {
describe("Create Authentication Requests", () => {
test("Should use default settings if none are supplied", () => {
var request = AuthenticationRequest.builder().build();

expect(request.getHost()).toBe('accounts.spotify.com');
expect(request.getHost()).toBe("accounts.spotify.com");
expect(request.getPort()).toBe(443);
expect(request.getScheme()).toBe('https');
expect(request.getScheme()).toBe("https");
expect(request.getHeaders()).toBeFalsy();
expect(request.getPath()).toBeFalsy();
expect(request.getQueryParameters()).toBeFalsy();
expect(request.getBodyParameters()).toBeFalsy();
});

test('Can overwrite one of the default parameters', () => {
test("Can overwrite one of the default parameters", () => {
var request = AuthenticationRequest.builder()
.withHost('such.host.wow')
.withHost("such.host.wow")
.build();

expect(request.getHost()).toBe('such.host.wow');
expect(request.getHost()).toBe("such.host.wow");
expect(request.getPort()).toBe(443);
expect(request.getScheme()).toBe('https');
expect(request.getScheme()).toBe("https");
expect(request.getHeaders()).toBeFalsy();
expect(request.getPath()).toBeFalsy();
expect(request.getQueryParameters()).toBeFalsy();
Expand Down
124 changes: 61 additions & 63 deletions __tests__/base-request.js
Original file line number Diff line number Diff line change
@@ -1,182 +1,180 @@
var Request = require('../src/base-request');
var Request = require("../src/base-request");

describe('Create Requests', () => {
test('Should create host, port, and scheme', () => {
describe("Create Requests", () => {
test("Should create host, port, and scheme", () => {
var request = Request.builder()
.withHost('such.api.wow')
.withHost("such.api.wow")
.withPort(1337)
.withScheme('http')
.withScheme("http")
.build();

expect(request.getHost()).toBe('such.api.wow');
expect(request.getHost()).toBe("such.api.wow");
expect(request.getPort()).toBe(1337);
expect(request.getScheme()).toBe('http');
expect(request.getScheme()).toBe("http");
});

test('Should add query parameters', () => {
test("Should add query parameters", () => {
var request = Request.builder()
.withHost('such.api.wow')
.withHost("such.api.wow")
.withPort(1337)
.withScheme('http')
.withScheme("http")
.withQueryParameters({
oneParameter: 1,
anotherParameter: true,
thirdParameter: 'hello'
thirdParameter: "hello",
})
.build();

expect(request.getQueryParameters().oneParameter).toBe(1);
expect(request.getQueryParameters().anotherParameter).toBe(true);
expect(request.getQueryParameters().thirdParameter).toBe('hello');
expect(request.getQueryParameters().thirdParameter).toBe("hello");
});

test('Should add query parameters (multiple calls)', () => {
test("Should add query parameters (multiple calls)", () => {
var request = Request.builder()
.withHost('such.api.wow')
.withHost("such.api.wow")
.withPort(1337)
.withScheme('http')
.withScheme("http")
.withQueryParameters({
oneParameter: 1,
anotherParameter: true
anotherParameter: true,
})
.withQueryParameters({
thirdParameter: 'hello'
thirdParameter: "hello",
})
.build();

expect(request.getQueryParameters().oneParameter).toBe(1);
expect(request.getQueryParameters().anotherParameter).toBe(true);
expect(request.getQueryParameters().thirdParameter).toBe('hello');
expect(request.getQueryParameters().thirdParameter).toBe("hello");
});

test('Should add query parameters (combine calls)', () => {
test("Should add query parameters (combine calls)", () => {
var request = Request.builder()
.withHost('such.api.wow')
.withHost("such.api.wow")
.withPort(1337)
.withScheme('http')
.withScheme("http")
.withQueryParameters(
{
oneParameter: 1,
anotherParameter: true
anotherParameter: true,
},
{
thirdParameter: 'hello'
thirdParameter: "hello",
}
)
.build();

expect(request.getQueryParameters().oneParameter).toBe(1);
expect(request.getQueryParameters().anotherParameter).toBe(true);
expect(request.getQueryParameters().thirdParameter).toBe('hello');
expect(request.getQueryParameters().thirdParameter).toBe("hello");
});

test('Should add body parameters', () => {
test("Should add body parameters", () => {
var request = Request.builder()
.withHost('such.api.wow')
.withHost("such.api.wow")
.withPort(1337)
.withScheme('http')
.withScheme("http")
.withBodyParameters({
one: 1,
two: true,
three: 'world'
three: "world",
})
.build();

expect(request.getBodyParameters().one).toBe(1);
expect(request.getBodyParameters().two).toBe(true);
expect(request.getBodyParameters().three).toBe('world');
expect(request.getBodyParameters().three).toBe("world");
});

test('Should add array to body parameters', () => {
test("Should add array to body parameters", () => {
var request = Request.builder()
.withHost('such.api.wow')
.withHost("such.api.wow")
.withPort(1337)
.withScheme('http')
.withBodyParameters(['3VNWq8rTnQG6fM1eldSpZ0'])
.withScheme("http")
.withBodyParameters(["3VNWq8rTnQG6fM1eldSpZ0"])
.build();

expect(request.getBodyParameters()).toEqual(['3VNWq8rTnQG6fM1eldSpZ0']);
expect(request.getBodyParameters()).toEqual(["3VNWq8rTnQG6fM1eldSpZ0"]);
});

test('Should add header parameters', () => {
test("Should add header parameters", () => {
var request = Request.builder()
.withHost('such.api.wow')
.withHost("such.api.wow")
.withPort(1337)
.withScheme('http')
.withScheme("http")
.withHeaders({
Authorization: 'Basic WOOP',
'Content-Type': 'application/lol'
Authorization: "Basic WOOP",
"Content-Type": "application/lol",
})
.build();

expect(request.getHeaders().Authorization).toBe('Basic WOOP');
expect(request.getHeaders()['Content-Type']).toBe('application/lol');
expect(request.getHeaders().Authorization).toBe("Basic WOOP");
expect(request.getHeaders()["Content-Type"]).toBe("application/lol");
});

test('Should add path', () => {
test("Should add path", () => {
var request = Request.builder()
.withHost('such.api.wow')
.withHost("such.api.wow")
.withPort(1337)
.withPath('/v1/users/meriosweg')
.withPath("/v1/users/meriosweg")
.build();

expect(request.getPath()).toBe('/v1/users/meriosweg');
expect(request.getPath()).toBe("/v1/users/meriosweg");
});

test('Should build URI', () => {
test("Should build URI", () => {
var request = Request.builder()
.withHost('such.api.wow')
.withScheme('https')
.withHost("such.api.wow")
.withScheme("https")
.withPort(1337)
.withPath('/v1/users/meriosweg')
.withPath("/v1/users/meriosweg")
.build();

expect(request.getURI()).toBe(
'https://such.api.wow:1337/v1/users/meriosweg'
"https://such.api.wow:1337/v1/users/meriosweg"
);
});

test('Should construct empty query paramaters string', () => {
var request = Request.builder()
.withQueryParameters({})
.build();
test("Should construct empty query paramaters string", () => {
var request = Request.builder().withQueryParameters({}).build();

expect(request.getQueryParameterString()).toBeFalsy();
});

test('Should construct query paramaters string for one parameter', () => {
test("Should construct query paramaters string for one parameter", () => {
var request = Request.builder()
.withQueryParameters({
one: 1
one: 1,
})
.build();

expect(request.getQueryParameterString()).toBe('?one=1');
expect(request.getQueryParameterString()).toBe("?one=1");
});

test('Should construct query paramaters string for multiple parameters', () => {
test("Should construct query paramaters string for multiple parameters", () => {
var request = Request.builder()
.withQueryParameters({
one: 1,
two: true,
three: 'world'
three: "world",
})
.build();

expect(request.getQueryParameterString()).toBe(
'?one=1&two=true&three=world'
"?one=1&two=true&three=world"
);
});

test('Should construct query paramaters string and exclude undefined values', () => {
test("Should construct query paramaters string and exclude undefined values", () => {
var request = Request.builder()
.withQueryParameters({
one: 1,
two: undefined,
three: 'world'
three: "world",
})
.build();

expect(request.getQueryParameterString()).toBe('?one=1&three=world');
expect(request.getQueryParameterString()).toBe("?one=1&three=world");
});
});
Loading

0 comments on commit cee94fd

Please sign in to comment.