Skip to content

Commit

Permalink
feat: add https support
Browse files Browse the repository at this point in the history
  • Loading branch information
mdasberg committed Mar 3, 2020
1 parent 7e62d7b commit 3eeab54
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/base.client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ describe('BaseClient', () => {

it('sets the baseUrl', () =>
expect(client.baseUrl).toBe('http://localhost:9000' + '/ngapimock'));

it('sets the https agent', () =>
expect((client as any).agent).toBeDefined());
});

describe('delayResponse', () => {
Expand Down Expand Up @@ -198,6 +201,7 @@ describe('BaseClient', () => {
client.invoke('some/query', 'GET', {some: 'body'});

assert.calledWith(fetchResponseFn, match(async (actual: Request) => {
expect((actual as any).agent).toBeUndefined();
expect(actual.method).toBe('GET');
expect(actual.url).toBe('http://localhost:9000/ngapimock/some/query');
expect(actual.headers.get('Cookie')).toBe('apimockid=123');
Expand All @@ -212,6 +216,7 @@ describe('BaseClient', () => {
client.invoke('some/query', 'DELETE', {some: 'body'});

assert.calledWith(fetchResponseFn, match(async (actual: Request) => {
expect((actual as any).agent).toBeUndefined();
expect(actual.method).toBe('DELETE');
expect(actual.url).toBe('http://localhost:9000/ngapimock/some/query');
expect(actual.headers.get('Cookie')).toBe('apimockid=123');
Expand All @@ -226,6 +231,7 @@ describe('BaseClient', () => {
client.invoke('some/query', 'POST', {some: 'body'});

assert.calledWith(fetchResponseFn, match(async (actual: Request) => {
expect((actual as any).agent).toBeUndefined();
expect(actual.method).toBe('POST');
expect(actual.url).toBe('http://localhost:9000/ngapimock/some/query');
expect(actual.headers.get('Cookie')).toBe('apimockid=123');
Expand All @@ -240,6 +246,7 @@ describe('BaseClient', () => {
client.invoke('some/query', 'PUT', {some: 'body'});

assert.calledWith(fetchResponseFn, match(async (actual: Request) => {
expect((actual as any).agent).toBeUndefined();
expect(actual.method).toBe('PUT');
expect(actual.url).toBe('http://localhost:9000/ngapimock/some/query');
expect(actual.headers.get('Cookie')).toBe('apimockid=123');
Expand All @@ -248,6 +255,20 @@ describe('BaseClient', () => {
}));
});
});

describe('adds the agent when https', () => {
beforeEach(()=> {
client.baseUrl = 'https://localhost:9000';

client.invoke('some/query', 'GET', {some: 'body'});
});

it('adds the agent to the request options', () => {
assert.calledWith(fetchResponseFn, match(async (actual: Request) => {
expect((actual as any).agent).toBeDefined();
}));
});
});
});


Expand Down
11 changes: 11 additions & 0 deletions src/base.client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import fetch, {Request} from 'node-fetch';
import * as uuid from 'uuid';
import {Client} from './client';
import * as https from "https";
import urljoin = require('url-join');

const COOKIE_NAME = 'apimockid';


/** Base client that takes care of the actual invoking of the ng-apimock api.*/
abstract class BaseClient implements Client {
public ngApimockId: string;
public baseUrl: string;
private agent: https.Agent;

/**
* Constructor.
Expand All @@ -17,6 +20,10 @@ abstract class BaseClient implements Client {
constructor(baseUrl: string) {
this.ngApimockId = uuid.v4();
this.baseUrl = urljoin(baseUrl, 'ngapimock');

this.agent = new https.Agent({
rejectUnauthorized: false
});
}

/**
Expand Down Expand Up @@ -112,6 +119,10 @@ abstract class BaseClient implements Client {
requestInit.body = JSON.stringify(body);
}

if (this.baseUrl.startsWith('https')) {
requestInit.agent = this.agent;
}

const url = urljoin(this.baseUrl, query);
return await this.fetchResponse(new Request(url, requestInit))
.then((response: Response) => {
Expand Down

0 comments on commit 3eeab54

Please sign in to comment.