Skip to content

Commit

Permalink
fix(test): convert to typescript
Browse files Browse the repository at this point in the history
EME-5329

Co-authored-by: Gabor Nemeth <gabor.nemeth@emarsys.com>
  • Loading branch information
sonicoder86 and ngabor84 committed Oct 21, 2022
1 parent 649a722 commit 80d66f5
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 146 deletions.
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "@emartech/escher-request",
"description": "Requests with Escher authentication",
"scripts": {
"test": "mocha --require ts-node/register ./src --recursive",
"test": "mocha --require ts-node/register --extension ts ./src --recursive",
"test:watch": "mocha --require ts-node/register --extension ts ./src --recursive --watch",
"lint": "eslint ./src/**/*.{ts,js}",
"build": "rm -rf dist && tsc --project ./tsconfig.json",
"release": "CI=true semantic-release"
Expand Down Expand Up @@ -33,7 +34,12 @@
"escher-auth": "3.2.4"
},
"devDependencies": {
"@types/node": "18.7.8",
"@types/chai": "4.3.3",
"@types/chai-subset": "1.3.3",
"@types/mocha": "10.0.0",
"@types/node": "18.7.23",
"@types/sinon": "10.0.13",
"@types/sinon-chai": "3.2.8",
"@typescript-eslint/parser": "5.35.1",
"chai": "4.3.6",
"chai-subset": "1.6.0",
Expand Down
37 changes: 0 additions & 37 deletions src/escher-auth.d.ts

This file was deleted.

20 changes: 10 additions & 10 deletions src/request.spec.js → src/request.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const sinon = require('sinon');
const { expect } = require('chai');
const axios = require('axios');
import sinon, { SinonStub } from 'sinon';
import { expect } from 'chai';
import axios from 'axios';
const Escher = require('escher-auth');
const http = require('http');
const https = require('https');
const { EscherRequest, EscherRequestOption } = require('./request');
import http from 'http';
import https from 'https';
import { EscherRequest, EscherRequestOption } from './request';

describe('EscherRequest', function() {
const serviceConfig = {
Expand All @@ -23,9 +23,9 @@ describe('EscherRequest', function() {
};
};

let requestOptions;
let requestStub;
let escherRequest;
let requestOptions: EscherRequestOption;
let requestStub: SinonStub;
let escherRequest: EscherRequest;

beforeEach(function() {
requestOptions = new EscherRequestOption(serviceConfig.host, serviceConfig);
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('EscherRequest', function() {
});

it('should sign headers with non string values', async () => {
requestOptions.setHeader(['x-customer-id', 15]);
requestOptions.setHeader(['x-customer-id', '15']);

await escherRequest.post('/path', { name: 'Almanach' });

Expand Down
8 changes: 4 additions & 4 deletions src/request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Escher from 'escher-auth';
const Escher = require('escher-auth');
import { Agent as HttpAgent } from 'http';
import { Agent as HttpsAgent } from 'https';
import { EscherRequestOption } from './requestOption';
Expand All @@ -17,10 +17,10 @@ export class EscherRequest {
authHeaderName: 'X-Ems-Auth',
dateHeaderName: 'X-Ems-Date'
};
private escher: Escher;
private escher: any;
private options: EscherRequestOption;
private readonly httpAgent?: HttpAgent;
private readonly httpsAgent?: HttpsAgent;
public readonly httpAgent?: HttpAgent;
public readonly httpsAgent?: HttpsAgent;

public static create(accessKeyId: string, apiSecret: string, requestOptions: EscherRequestOption) {
return new EscherRequest(accessKeyId, apiSecret, requestOptions);
Expand Down
18 changes: 8 additions & 10 deletions src/requestError.spec.js → src/requestError.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { expect } = require('chai');
const { EscherRequestError } = require('./requestError');
import { expect } from 'chai';
import { EscherRequestError } from './requestError';

describe('EscherRequestError', function() {
it('should extend base Error class', function() {
Expand All @@ -10,6 +10,10 @@ describe('EscherRequestError', function() {

it('should store constructor parameters', function() {
const error = new EscherRequestError('Invalid request', 400, {
status: 200,
statusText: 'OK',
headers: {},
config: {},
data: {
replyText: 'Too long',
detailedMessage: 'Line too long'
Expand All @@ -26,17 +30,11 @@ describe('EscherRequestError', function() {
});

it('should store response as is when no data attribute present', function() {
const error = new EscherRequestError('Invalid request', 400, {
replyText: 'Too long',
detailedMessage: 'Line too long'
});
const error = new EscherRequestError('Invalid request', 400, 'Line too long');

expect(error.message).to.eql('Invalid request');
expect(error.code).to.eql(400);
expect(error.data).to.eql({
replyText: 'Too long',
detailedMessage: 'Line too long'
});
expect(error.data).to.eql('Line too long');
});

it('should always contain data on error', function() {
Expand Down
36 changes: 19 additions & 17 deletions src/requestOption.spec.js → src/requestOption.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { expect } = require('chai');
const { EscherRequestOption } = require('./requestOption');
import { expect } from 'chai';
import { EscherRequestOption, RequestOptions } from './requestOption';

describe('EscherRequestOption', function() {
let dummyServiceConfig;
let dummyServiceConfig: RequestOptions;
const host = 'localhost';

beforeEach(function() {
dummyServiceConfig = {
Expand Down Expand Up @@ -53,22 +54,22 @@ describe('EscherRequestOption', function() {

it('can accept additional headers', function() {
const dummyHeader = ['header-name', 'header-value'];
const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig);
const requestOptions = new EscherRequestOption(host, dummyServiceConfig);

requestOptions.setHeader(dummyHeader);

expect(requestOptions.getHeader('header-name')).to.eql('header-value');
});

it('should add default content type', function() {
const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig);
const requestOptions = new EscherRequestOption(host, dummyServiceConfig);

expect(requestOptions.getHeader('content-type')).to.eql('application/json');
});

it('should not duplicate headers with same name', function() {
const expectedContentTypeHeader = ['content-type', 'text/csv'];
const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig);
const requestOptions = new EscherRequestOption(host, dummyServiceConfig);

requestOptions.setHeader(expectedContentTypeHeader);

Expand All @@ -79,36 +80,36 @@ describe('EscherRequestOption', function() {

describe('allowEmptyResponse', function() {
it('should be set to false by default', function() {
const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig);
const requestOptions = new EscherRequestOption(host, dummyServiceConfig);

expect(requestOptions.allowEmptyResponse).to.eql(false);
});

it('should be set to the value provided in config', function() {
dummyServiceConfig.allowEmptyResponse = true;
const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig);
const requestOptions = new EscherRequestOption(host, dummyServiceConfig);

expect(requestOptions.allowEmptyResponse).to.eql(true);
});
});

describe('timeout', function() {
it('should return a default value', function() {
const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig);
const requestOptions = new EscherRequestOption(host, dummyServiceConfig);

expect(requestOptions.getTimeout()).to.be.eql(15000);
});

it('should return the timeout passed in the constructor', function() {
const options = Object.assign({}, dummyServiceConfig);
options.timeout = 0;
const requestOptions = new EscherRequestOption(dummyServiceConfig.host, options);
const requestOptions = new EscherRequestOption(host, options);

expect(requestOptions.getTimeout()).to.be.eql(0);
});

it('should return the timeout set by setTimeout', function() {
const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig);
const requestOptions = new EscherRequestOption(host, dummyServiceConfig);

requestOptions.setTimeout(60000);

Expand All @@ -120,7 +121,7 @@ describe('EscherRequestOption', function() {
describe('toHash', function() {

it('should return the proper object', function() {
const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig);
const requestOptions = new EscherRequestOption(host, dummyServiceConfig);

expect(requestOptions.toHash()).to.be.eql({
headers: [
Expand All @@ -139,21 +140,22 @@ describe('EscherRequestOption', function() {

it('should add allowEmptyResponse to hash if set to TRUE', function() {
dummyServiceConfig.allowEmptyResponse = true;
const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig);
const requestOptions = new EscherRequestOption(host, dummyServiceConfig);

expect(requestOptions.toHash()).to.have.property('allowEmptyResponse', true);
});

it('should not cache headers', function() {
const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig);
requestOptions.toHash().headers.push('from_test');
expect(requestOptions.toHash().headers).not.to.include('from_test');
const requestOptions = new EscherRequestOption(host, dummyServiceConfig);
const headers = requestOptions.toHash().headers as string[][];
headers.push(['from_test']);
expect(requestOptions.toHash().headers).not.to.include(['from_test']);
});
});

describe('setHost', () => {
it('should set host', () => {
const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig);
const requestOptions = new EscherRequestOption(host, dummyServiceConfig);

requestOptions.setHost('suitedocker.ett.local');

Expand Down
11 changes: 11 additions & 0 deletions src/setup.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import sinon from 'sinon';
import chai from 'chai';
import chaiSubset from 'chai-subset';
import chaiSinon from 'sinon-chai';

chai.use(chaiSinon);
chai.use(chaiSubset);

afterEach(function() {
sinon.restore();
});
13 changes: 0 additions & 13 deletions src/testSetup.spec.js

This file was deleted.

Loading

0 comments on commit 80d66f5

Please sign in to comment.