Comparing version 3.0.0 to 3.0.1
# Changelog | ||
## 3.0.1 | ||
* Separated error handling responsibility between postmark client and http client | ||
## 3.0.0 | ||
@@ -4,0 +8,0 @@ |
@@ -70,8 +70,5 @@ "use strict"; | ||
BaseClient.prototype.processHttpRequest = function (method, path, queryParameters, body) { | ||
var _this = this; | ||
return this.httpClient.httpRequest(method, path, queryParameters, body, this.getComposedHttpRequestHeaders()) | ||
.then(function (response) { return response; }) | ||
.catch(function (error) { | ||
throw _this.errorHandler.buildRequestError(error); | ||
}); | ||
.catch(function (error) { return Promise.reject(error); }); | ||
}; | ||
@@ -109,3 +106,3 @@ /** | ||
if (!token || token.trim() === "") { | ||
throw this.errorHandler.buildGeneralError("A valid API token must be provided."); | ||
throw this.errorHandler.buildError("A valid API token must be provided."); | ||
} | ||
@@ -112,0 +109,0 @@ }; |
@@ -1,2 +0,1 @@ | ||
import { HttpClientError } from "../models"; | ||
import * as Errors from "./Errors"; | ||
@@ -9,10 +8,2 @@ /** | ||
/** | ||
* Process callback function for HTTP request. | ||
* | ||
* @param error - request error that needs to be transformed to proper Postmark error. | ||
* | ||
* @return {PostmarkError} - formatted Postmark error | ||
*/ | ||
buildRequestError(error: HttpClientError): Errors.PostmarkError; | ||
/** | ||
* Build general Postmark error. | ||
@@ -24,12 +15,4 @@ * | ||
*/ | ||
buildGeneralError(errorMessage: string): Errors.PostmarkError; | ||
buildError(errorMessage: string, code?: number, statusCode?: number): Errors.PostmarkError | Errors.HttpError; | ||
/** | ||
* Build Postmark error based on response from http client. | ||
* | ||
* @param {AxiosResponse} response - request response used to transform to Postmark error. | ||
* @return {PostmarkError} - formatted Postmark error | ||
*/ | ||
private buildErrorForResponse; | ||
private retrieveDefaultOrValue; | ||
/** | ||
* Build Postmark error based on HTTP request status. | ||
@@ -41,3 +24,3 @@ * | ||
*/ | ||
private buildRequestErrorByStatus; | ||
private buildErrorByHttpStatusCode; | ||
} |
@@ -13,47 +13,19 @@ "use strict"; | ||
/** | ||
* Process callback function for HTTP request. | ||
* Build general Postmark error. | ||
* | ||
* @param error - request error that needs to be transformed to proper Postmark error. | ||
* @param errorMessage - error message that needs to be identified and transformed to proper Postmark error. | ||
* | ||
* @return {PostmarkError} - formatted Postmark error | ||
* @returns properly formatted Postmark error. | ||
*/ | ||
ErrorHandler.prototype.buildRequestError = function (error) { | ||
var response = error.response; | ||
if (response !== undefined) { | ||
return this.buildErrorForResponse(response, error.message); | ||
ErrorHandler.prototype.buildError = function (errorMessage, code, statusCode) { | ||
if (code === void 0) { code = 0; } | ||
if (statusCode === void 0) { statusCode = 0; } | ||
if (statusCode === 0 && code === 0) { | ||
return new Errors.PostmarkError(errorMessage); | ||
} | ||
else if (error.message !== undefined) { | ||
return this.buildGeneralError(error.message); | ||
} | ||
else { | ||
return this.buildGeneralError(JSON.stringify(error, Object.getOwnPropertyNames(error))); | ||
return this.buildErrorByHttpStatusCode(errorMessage, code, statusCode); | ||
} | ||
}; | ||
/** | ||
* Build general Postmark error. | ||
* | ||
* @param errorMessage - error message that needs to be identified and transformed to proper Postmark error. | ||
* | ||
* @returns properly formatted Postmark error. | ||
*/ | ||
ErrorHandler.prototype.buildGeneralError = function (errorMessage) { | ||
return new Errors.PostmarkError(errorMessage); | ||
}; | ||
/** | ||
* Build Postmark error based on response from http client. | ||
* | ||
* @param {AxiosResponse} response - request response used to transform to Postmark error. | ||
* @return {PostmarkError} - formatted Postmark error | ||
*/ | ||
ErrorHandler.prototype.buildErrorForResponse = function (response, errorMessage) { | ||
var data = response.data; | ||
var status = this.retrieveDefaultOrValue(0, response.status); | ||
var errorCode = this.retrieveDefaultOrValue(0, data.ErrorCode); | ||
var message = this.retrieveDefaultOrValue(errorMessage, data.Message); | ||
return this.buildRequestErrorByStatus(message, errorCode, status); | ||
}; | ||
ErrorHandler.prototype.retrieveDefaultOrValue = function (defaultValue, data) { | ||
return (data === undefined) ? defaultValue : data; | ||
}; | ||
/** | ||
* Build Postmark error based on HTTP request status. | ||
@@ -65,3 +37,3 @@ * | ||
*/ | ||
ErrorHandler.prototype.buildRequestErrorByStatus = function (errorMessage, errorCode, errorStatusCode) { | ||
ErrorHandler.prototype.buildErrorByHttpStatusCode = function (errorMessage, errorCode, errorStatusCode) { | ||
switch (errorStatusCode) { | ||
@@ -68,0 +40,0 @@ case 401: |
import { AxiosInstance } from "axios"; | ||
import { ClientOptions, HttpClient } from "./models"; | ||
export declare class AxiosHttpClient extends HttpClient { | ||
protected client: AxiosInstance; | ||
client: AxiosInstance; | ||
private errorHandler; | ||
constructor(configOptions?: ClientOptions.Configuration); | ||
/** | ||
@@ -21,2 +23,10 @@ * Create http client instance with default settings. | ||
/** | ||
* Process callback function for HTTP request. | ||
* | ||
* @param error - request error that needs to be transformed to proper Postmark error. | ||
* | ||
* @return {PostmarkError} - formatted Postmark error | ||
*/ | ||
private transformError; | ||
/** | ||
* Timeout in seconds is adjusted to Axios format. | ||
@@ -27,2 +37,3 @@ * | ||
private getRequestTimeoutInSeconds; | ||
private adjustValue; | ||
} |
@@ -32,6 +32,9 @@ "use strict"; | ||
var models_1 = require("./models"); | ||
var index_1 = require("./errors/index"); | ||
var AxiosHttpClient = /** @class */ (function (_super) { | ||
__extends(AxiosHttpClient, _super); | ||
function AxiosHttpClient() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
function AxiosHttpClient(configOptions) { | ||
var _this = _super.call(this, configOptions) || this; | ||
_this.errorHandler = new index_1.ErrorHandler(); | ||
return _this; | ||
} | ||
@@ -67,2 +70,3 @@ /** | ||
AxiosHttpClient.prototype.httpRequest = function (method, path, queryParameters, body, requestHeaders) { | ||
var _this = this; | ||
return this.client.request({ | ||
@@ -74,5 +78,29 @@ method: method, | ||
params: queryParameters, | ||
}).catch(function (errorThrown) { | ||
return Promise.reject(_this.transformError(errorThrown)); | ||
}); | ||
}; | ||
/** | ||
* Process callback function for HTTP request. | ||
* | ||
* @param error - request error that needs to be transformed to proper Postmark error. | ||
* | ||
* @return {PostmarkError} - formatted Postmark error | ||
*/ | ||
AxiosHttpClient.prototype.transformError = function (errorThrown) { | ||
var response = errorThrown.response; | ||
if (response !== undefined) { | ||
var status_1 = this.adjustValue(0, response.status); | ||
var errorCode = this.adjustValue(0, response.data.ErrorCode); | ||
var message = this.adjustValue(errorThrown.message, response.data.Message); | ||
return this.errorHandler.buildError(message, errorCode, status_1); | ||
} | ||
else if (errorThrown.message !== undefined) { | ||
return this.errorHandler.buildError(errorThrown.message); | ||
} | ||
else { | ||
return this.errorHandler.buildError(JSON.stringify(errorThrown, Object.getOwnPropertyNames(errorThrown))); | ||
} | ||
}; | ||
/** | ||
* Timeout in seconds is adjusted to Axios format. | ||
@@ -85,2 +113,5 @@ * | ||
}; | ||
AxiosHttpClient.prototype.adjustValue = function (defaultValue, data) { | ||
return (data === undefined) ? defaultValue : data; | ||
}; | ||
return AxiosHttpClient; | ||
@@ -87,0 +118,0 @@ }(models_1.HttpClient)); |
import { ClientOptions } from "./ClientOptions"; | ||
/** | ||
* Minimum data that has to be provided by HttpClient error | ||
* so that Error can be classified. | ||
*/ | ||
export interface HttpClientError<T = any> extends Error { | ||
response?: HttpClientErrorResponse<T>; | ||
} | ||
/** | ||
* Minimum data that has to be provided by HttpClient error response | ||
* so that Error can be classified. | ||
*/ | ||
export interface HttpClientErrorResponse<T = any> { | ||
data: T; | ||
status: number; | ||
} | ||
export declare abstract class HttpClient { | ||
@@ -25,4 +10,4 @@ /** | ||
clientOptions: ClientOptions.Configuration; | ||
protected client: any; | ||
constructor(configOptions?: ClientOptions.Configuration); | ||
client: any; | ||
protected constructor(configOptions?: ClientOptions.Configuration); | ||
getBaseHttpRequestURL(): string; | ||
@@ -29,0 +14,0 @@ abstract initHttpClient(configOptions?: ClientOptions.Configuration): void; |
@@ -12,3 +12,3 @@ { | ||
], | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"author": "Igor Balos", | ||
@@ -15,0 +15,0 @@ "contributors": [ |
@@ -8,2 +8,3 @@ import * as postmark from "../../src/index"; | ||
import * as sinon from "sinon"; | ||
import {InternalServerError} from "../../src/client/errors/Errors"; | ||
const testingKeys = nconf.env().file({ file: __dirname + "/../../testing_keys.json" }); | ||
@@ -35,2 +36,6 @@ | ||
}); | ||
it("httpClient initialized", () => { | ||
expect(client.httpClient.client.defaults).not.to.eql(180000); | ||
}); | ||
}); | ||
@@ -58,12 +63,2 @@ | ||
/* | ||
it("getComposedHttpRequestHeaders", () => { | ||
expect(client.httpClient.getComposedHttpRequestHeaders()).to.eql({ | ||
"X-Postmark-Account-Token": accountToken, | ||
"Accept": "application/json", | ||
"User-Agent": `Postmark.JS - ${clientVersion}`, | ||
}); | ||
}); | ||
*/ | ||
it("set clientOptions timeout", () => { | ||
@@ -91,3 +86,14 @@ const timeoutValue: number = 10; | ||
describe("errors", () => { | ||
it("set clientOptions requestHost", () => { | ||
const host: string = "test.com"; | ||
client.setClientOptions({requestHost: host}); | ||
expect(client.getClientOptions()).to.eql({ | ||
useHttps: true, | ||
requestHost: host, | ||
timeout: 180, | ||
}); | ||
}); | ||
describe("invalid requests", () => { | ||
it("empty token", () => { | ||
@@ -100,3 +106,3 @@ expect(() => new postmark.AccountClient("")) | ||
const errorType = "InternalServerError"; | ||
const rejectError = {response: {status: 500, data: "response"}}; | ||
const rejectError = new InternalServerError("response", 500, 500); | ||
let sandbox: sinon.SinonSandbox; | ||
@@ -112,3 +118,3 @@ | ||
it("promise error", () => { | ||
it("promise error",() => { | ||
client = new postmark.AccountClient(serverToken); | ||
@@ -124,3 +130,3 @@ sandbox.stub(client.httpClient, "httpRequest").rejects(rejectError); | ||
it("callback error", (done) => { | ||
it("callback error",(done) => { | ||
client = new postmark.AccountClient("testToken"); | ||
@@ -137,2 +143,49 @@ sandbox.stub(client.httpClient, "httpRequest").rejects(rejectError); | ||
}); | ||
describe("valid requests", () => { | ||
describe("httpRequest", () => { | ||
const errorType = "InternalServerError"; | ||
const rejectError = new InternalServerError("response", 500, 500); | ||
let sandbox: sinon.SinonSandbox; | ||
beforeEach(() => { | ||
sandbox = sinon.createSandbox(); | ||
}); | ||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
it("promise result",() => { | ||
const successMessage = "success!" | ||
client = new postmark.AccountClient(serverToken); | ||
sandbox.stub(client.httpClient, "httpRequest").resolves(successMessage); | ||
return client.getSenderSignatures().then((result) => { | ||
expect(result).to.equal(successMessage) | ||
}, (error) => { | ||
throw Error(`Should not be here with error: ${error}`); | ||
}); | ||
}); | ||
it("callback result",(done) => { | ||
const successMessage = "success!" | ||
sandbox.stub(client.httpClient, "httpRequest").resolves(successMessage); | ||
client.getSenderSignatures(undefined, (error: any, data) => { | ||
expect(data).to.equal(successMessage); | ||
expect(error).to.equal(null); | ||
done(); | ||
}); | ||
}); | ||
it("callback called once", async() => { | ||
const callback = sinon.spy(); | ||
sandbox.stub(client.httpClient, "httpRequest").resolves("test"); | ||
await client.getSenderSignatures(undefined, callback); | ||
expect(callback.calledOnce).to.be.true | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -8,10 +8,9 @@ import { Errors } from "../../src"; | ||
describe("ErrorHandler", () => { | ||
it("buildGeneralError", () => { | ||
it("buildError", () => { | ||
const errorHandler = new ErrorHandler(); | ||
const error = new Error(); | ||
const postmarkError = errorHandler.buildError("Test message"); | ||
error.name = "Test name"; | ||
error.message = "Test message"; | ||
const postmarkError = errorHandler.buildGeneralError("Test message"); | ||
expect(postmarkError.message).to.equal(error.message); | ||
@@ -24,37 +23,19 @@ expect(postmarkError.name).to.equal("PostmarkError"); | ||
const error: any = { message: 'Hello Error' }; | ||
const postmarkError = errorHandler.buildError(error.message); | ||
const postmarkError = errorHandler.buildRequestError(error); | ||
expect(postmarkError).to.be.an.instanceof(Errors.PostmarkError); | ||
expect(postmarkError.name).to.equal("PostmarkError"); | ||
expect(postmarkError.message).to.equal(error.message); | ||
expect(postmarkError.code).to.equal(0); | ||
expect(postmarkError.statusCode).to.equal(0) | ||
}); | ||
it("no message", () => { | ||
const errorHandler = new ErrorHandler(); | ||
const error: any = { stack: 'Hello stack' }; | ||
const postmarkError = errorHandler.buildRequestError(error); | ||
expect(postmarkError).to.be.an.instanceof(Errors.PostmarkError); | ||
expect(postmarkError.name).to.equal("PostmarkError"); | ||
expect(postmarkError.message).to.equal(JSON.stringify(error)); | ||
}); | ||
describe("statuses", () => { | ||
it("401", () => { | ||
const errorHandler = new ErrorHandler(); | ||
const postmarkError = errorHandler.buildError("Test message", 401, 401); | ||
const error: any = { | ||
response: { | ||
data: { | ||
Message: "Test message", | ||
ErrorCode: 401, | ||
}, | ||
status: 401, | ||
} | ||
}; | ||
const postmarkError = errorHandler.buildRequestError(error); | ||
expect(postmarkError).to.be.an.instanceof(Errors.InvalidAPIKeyError); | ||
expect(postmarkError.name).to.equal("InvalidAPIKeyError"); | ||
expect(postmarkError.message).to.equal(error.response.data.Message); | ||
expect(postmarkError.message).to.equal("Test message"); | ||
}); | ||
@@ -64,17 +45,9 @@ | ||
const errorHandler = new ErrorHandler(); | ||
const postmarkError = errorHandler.buildError("Test message", 423, 422); | ||
const error: any = { | ||
response: { | ||
data: { | ||
Message: "Test message", | ||
ErrorCode: 422, | ||
}, | ||
status: 422, | ||
} | ||
}; | ||
const postmarkError = errorHandler.buildRequestError(error); | ||
expect(postmarkError).to.be.an.instanceof(Errors.ApiInputError); | ||
expect(postmarkError.name).to.equal("ApiInputError"); | ||
expect(postmarkError.message).to.equal(error.response.data.Message); | ||
expect(postmarkError.message).to.equal("Test message"); | ||
expect(postmarkError.code).to.equal(423); | ||
expect(postmarkError.statusCode).to.equal(422); | ||
}); | ||
@@ -84,17 +57,7 @@ | ||
const errorHandler = new ErrorHandler(); | ||
const postmarkError = errorHandler.buildError("Test message", 429, 429); | ||
const error: any = { | ||
response: { | ||
data: { | ||
Message: "Test message", | ||
ErrorCode: 429, | ||
}, | ||
status: 429, | ||
} | ||
}; | ||
const postmarkError = errorHandler.buildRequestError(error); | ||
expect(postmarkError).to.be.an.instanceof(Errors.RateLimitExceededError); | ||
expect(postmarkError.name).to.equal("RateLimitExceededError"); | ||
expect(postmarkError.message).to.equal(error.response.data.Message); | ||
expect(postmarkError.message).to.equal("Test message"); | ||
}); | ||
@@ -104,17 +67,7 @@ | ||
const errorHandler = new ErrorHandler(); | ||
const postmarkError = errorHandler.buildError("Test message", 500, 500); | ||
const error: any = { | ||
response: { | ||
data: { | ||
Message: "Test message", | ||
ErrorCode: 500, | ||
}, | ||
status: 500, | ||
} | ||
}; | ||
const postmarkError = errorHandler.buildRequestError(error); | ||
expect(postmarkError).to.be.an.instanceof(Errors.InternalServerError); | ||
expect(postmarkError.name).to.equal("InternalServerError"); | ||
expect(postmarkError.message).to.equal(error.response.data.Message); | ||
expect(postmarkError.message).to.equal("Test message"); | ||
}); | ||
@@ -124,34 +77,25 @@ | ||
const errorHandler = new ErrorHandler(); | ||
const postmarkError = errorHandler.buildError("Test message", 600, 600); | ||
const error: any = { | ||
response: { | ||
data: { | ||
Message: "Test message", | ||
ErrorCode: 600, | ||
}, | ||
status: 600, | ||
} | ||
}; | ||
const postmarkError = errorHandler.buildRequestError(error); | ||
expect(postmarkError).to.be.an.instanceof(Errors.PostmarkError); | ||
expect(postmarkError.name).to.equal("UnknownError"); | ||
expect(postmarkError.message).to.equal(error.response.data.Message); | ||
expect(postmarkError.message).to.equal("Test message"); | ||
}); | ||
it("no status", () => { | ||
it("no http status", () => { | ||
const errorHandler = new ErrorHandler(); | ||
const postmarkError = errorHandler.buildError("Test message"); | ||
const error: any = { | ||
response: { | ||
data: { | ||
Message: "Test message" | ||
} | ||
} | ||
}; | ||
expect(postmarkError).to.be.an.instanceof(Errors.PostmarkError); | ||
expect(postmarkError.name).to.equal("PostmarkError"); | ||
expect(postmarkError.message).to.equal("Test message"); | ||
}); | ||
const postmarkError = errorHandler.buildRequestError(error); | ||
it("unknown http status", () => { | ||
const errorHandler = new ErrorHandler(); | ||
const postmarkError = errorHandler.buildError("Test message", 500, 15); | ||
expect(postmarkError).to.be.an.instanceof(Errors.PostmarkError); | ||
expect(postmarkError.name).to.equal("UnknownError"); | ||
expect(postmarkError.message).to.equal(error.response.data.Message); | ||
expect(postmarkError.message).to.equal("Test message"); | ||
}); | ||
@@ -161,4 +105,4 @@ | ||
const errorHandler = new ErrorHandler(); | ||
const postmarkError = errorHandler.buildError("Test message"); | ||
const postmarkError = errorHandler.buildGeneralError("Test message"); | ||
expect(postmarkError).to.be.an.instanceof(Errors.PostmarkError); | ||
@@ -175,16 +119,11 @@ expect(postmarkError.name).to.equal("PostmarkError"); | ||
const error: any = { | ||
response: { | ||
data: { | ||
Message: "InactiveRecipientsError: You tried to send to recipients " + | ||
message: "InactiveRecipientsError: You tried to send to recipients " + | ||
"that have all been marked as inactive.\n" + | ||
"Found inactive addresses: nothing2@example.com, nothing@example.com.\n" + | ||
"Inactive recipients are ones that have generated a hard bounce, " + | ||
"a spam complaint, or a manual suppression.\n", | ||
ErrorCode: 406, | ||
}, | ||
status: 422, | ||
} | ||
"a spam complaint, or a manual suppression.\n", errorCode: 406, status: 422 | ||
}; | ||
const postmarkError: any = errorHandler.buildRequestError(error); | ||
const postmarkError:any = errorHandler.buildError(error.message, error.errorCode, error.status); | ||
expect(postmarkError).to.be.an.instanceof(Errors.InactiveRecipientsError); | ||
@@ -195,5 +134,12 @@ expect(postmarkError.name).to.equal("InactiveRecipientsError"); | ||
it("parse some inactive recipients", () => { | ||
it("email error", () => { | ||
const errorHandler = new ErrorHandler(); | ||
const error: any = { message: "Error message", errorCode: 300, status: 422 }; | ||
const postmarkError:any = errorHandler.buildError(error.message, error.errorCode, error.status); | ||
expect(postmarkError).to.be.an.instanceof(Errors.InvalidEmailRequestError); | ||
expect(postmarkError.name).to.equal("InvalidEmailRequestError"); | ||
}); | ||
it("parse some inactive recipients", () => { | ||
const message = "Message OK, but will not deliver to these inactive addresses: " + | ||
@@ -208,4 +154,2 @@ "nothing2@example.com, nothing@example.com" | ||
it("parse some inactive recipients - different error message", () => { | ||
const errorHandler = new ErrorHandler(); | ||
const message = "Message OK, but will not deliver to these inactive addresses: " + | ||
@@ -212,0 +156,0 @@ "nothing2@example.com, nothing@example.com. Inactive addresses can be activated." |
@@ -13,3 +13,3 @@ import * as postmark from "../../src/index"; | ||
it("#new ServerClient", () => { | ||
let client = new postmark.ServerClient(serverToken); | ||
const client = new postmark.ServerClient(serverToken); | ||
expect(client).not.to.equal(undefined); | ||
@@ -19,3 +19,3 @@ }); | ||
it("#new AccountClient", () => { | ||
let client = new postmark.AccountClient(serverToken); | ||
const client = new postmark.AccountClient(serverToken); | ||
expect(client).not.to.equal(undefined); | ||
@@ -27,3 +27,3 @@ expect(client).to.be.instanceOf(postmark.AccountClient) | ||
it("#new Client", () => { | ||
let client = new postmark.Client(serverToken); | ||
const client = new postmark.Client(serverToken); | ||
expect(client).not.to.equal(undefined); | ||
@@ -34,3 +34,3 @@ expect(client).to.be.instanceOf(postmark.ServerClient) | ||
it("#new AdminClient", () => { | ||
let client = new postmark.AdminClient(serverToken); | ||
const client = new postmark.AdminClient(serverToken); | ||
expect(client).not.to.equal(undefined); | ||
@@ -37,0 +37,0 @@ expect(client).to.be.instanceOf(postmark.AccountClient) |
@@ -12,2 +12,3 @@ import * as postmark from "../../src/index"; | ||
import * as sinon from "sinon"; | ||
import {InternalServerError} from "../../src/client/errors/Errors"; | ||
@@ -31,2 +32,6 @@ describe("ServerClient", () => { | ||
}); | ||
it("httpClient initialized", () => { | ||
expect(client.httpClient.client.defaults).not.to.eql(180000); | ||
}); | ||
}); | ||
@@ -40,11 +45,2 @@ | ||
/* | ||
it("getComposedHttpRequestHeaders", () => { | ||
expect(client.httpClient.getComposedHttpRequestHeaders()).to.eql({ | ||
"X-Postmark-Server-Token": serverToken, | ||
"Accept": "application/json", | ||
"User-Agent": `Postmark.JS - ${clientVersion}`, | ||
}); | ||
}); | ||
*/ | ||
describe("clientOptions", () => { | ||
@@ -117,5 +113,15 @@ it("clientOptions=", () => { | ||
it("set clientOptions requestHost", () => { | ||
const host: string = "test.com"; | ||
client.setClientOptions({requestHost: host}); | ||
expect(client.getClientOptions()).to.eql({ | ||
useHttps: true, | ||
requestHost: host, | ||
timeout: 180, | ||
}); | ||
}); | ||
}); | ||
describe("requests", () => { | ||
describe("valid requests", () => { | ||
let sandbox: sinon.SinonSandbox; | ||
@@ -131,6 +137,6 @@ | ||
describe("callback", () => { | ||
it("process it when there are no errors", async() => { | ||
let callback = sinon.spy(); | ||
sandbox.stub(client.httpClient, "httpRequest").returns(Promise.resolve("test")); | ||
describe("httpRequest", () => { | ||
it("callback called once", async() => { | ||
const callback = sinon.spy(); | ||
sandbox.stub(client.httpClient, "httpRequest").resolves("test"); | ||
@@ -141,7 +147,19 @@ await client.getServer(callback); | ||
it("process regular response based on request status", () => { | ||
sandbox.stub(client.httpClient, "httpRequest").returns(Promise.resolve("test")); | ||
it("callback result",(done) => { | ||
const successMessage = "success!" | ||
sandbox.stub(client.httpClient, "httpRequest").resolves(successMessage); | ||
client.getServer((error: any, data) => { | ||
expect(data).to.equal(successMessage); | ||
expect(error).to.equal(null); | ||
done(); | ||
}); | ||
}); | ||
it("promise result", () => { | ||
const successMessage = "success!" | ||
sandbox.stub(client.httpClient, "httpRequest").resolves(successMessage); | ||
return client.getServer().then((result) => { | ||
expect(result).to.eq("test"); | ||
expect(result).to.eq(successMessage); | ||
}, (error) => { | ||
@@ -151,15 +169,48 @@ throw Error(`Should not be here with error: ${error}`); | ||
}); | ||
it("process error response based on request status", () => { | ||
sandbox.stub(client.httpClient, "httpRequest").rejects({response: {status: 600, data: "response"}}); | ||
}); | ||
}); | ||
describe("invalid requests", () => { | ||
let sandbox: sinon.SinonSandbox; | ||
beforeEach(() => { | ||
sandbox = sinon.createSandbox(); | ||
}); | ||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
it("empty token", () => { | ||
expect(() => new postmark.ServerClient("")) | ||
.to.throw("A valid API token must be provided."); | ||
}); | ||
describe("httpRequest", () => { | ||
it("promise error", () => { | ||
const rejectError = new InternalServerError("response", 500, 500); | ||
sandbox.stub(client.httpClient, "httpRequest").rejects(rejectError); | ||
return client.getServer().then((result) => { | ||
throw Error(`Should not be here with result: ${result}`); | ||
}, (error) => { | ||
expect(error.name).to.eq("UnknownError"); | ||
expect(error.name).to.eq(rejectError.name); | ||
}); | ||
}); | ||
it("callback error",(done) => { | ||
const errorType = "InternalServerError"; | ||
const rejectError = new InternalServerError("response", 500, 500); | ||
client = new postmark.ServerClient("testToken"); | ||
sandbox.stub(client.httpClient, "httpRequest").rejects(rejectError); | ||
client.getServer((error: any, data) => { | ||
expect(data).to.equal(null); | ||
expect(error.name).to.equal(errorType); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
313082
149
5430