diff --git a/src/OnspringClient.ts b/src/OnspringClient.ts index e425cb0..bc3bd1b 100644 --- a/src/OnspringClient.ts +++ b/src/OnspringClient.ts @@ -1,6 +1,7 @@ import { AxiosInstance } from 'axios'; import axios from 'axios'; import { ArgumentValidator } from './models/ArgumentValidator'; +import { EndpointFactory } from './models/EndpointFactory'; /** * @class OnspringClient - A client that can communicate with the Onspring API. @@ -39,4 +40,19 @@ export class OnspringClient { }, }); } + + public async canConnect(): Promise { + const endpoint = EndpointFactory.getPingEndpoint(this._client.defaults.baseURL); + try { + var response = await this._client.get(endpoint); + return response.status === 200; + } catch (error) { + return false; + } + } + + private async get(endpoint: string): Promise { + const response = await this._client.get(endpoint); + return response.data; + } } diff --git a/src/enums/HttpStatusCode.ts b/src/enums/HttpStatusCode.ts new file mode 100644 index 0000000..973091b --- /dev/null +++ b/src/enums/HttpStatusCode.ts @@ -0,0 +1,17 @@ +export enum HttpStatusCode { + OK = 200, + Created = 201, + Accepted = 202, + NoContent = 204, + BadRequest = 400, + Unauthorized = 401, + Forbidden = 403, + NotFound = 404, + MethodNotAllowed = 405, + Conflict = 409, + InternalServerError = 500, + NotImplemented = 501, + BadGateway = 502, + ServiceUnavailable = 503, + GatewayTimeout = 504, +} diff --git a/tests/HttpStatusCode.spec.ts b/tests/HttpStatusCode.spec.ts new file mode 100644 index 0000000..92c2107 --- /dev/null +++ b/tests/HttpStatusCode.spec.ts @@ -0,0 +1,109 @@ +import { HttpStatusCode } from '../src/enums/HttpStatusCode'; +import { expect } from 'chai'; + +describe('HttpStatusCode', () => { + describe('OK', () => { + it('should return the correct value', () => { + const result = HttpStatusCode.OK; + expect(result).to.equal(200); + }); + }); + + describe('Created', () => { + it('should return the correct value', () => { + const result = HttpStatusCode.Created; + expect(result).to.equal(201); + }); + }); + + describe('Accepted', () => { + it('should return the correct value', () => { + const result = HttpStatusCode.Accepted; + expect(result).to.equal(202); + }); + }); + + describe('NoContent', () => { + it('should return the correct value', () => { + const result = HttpStatusCode.NoContent; + expect(result).to.equal(204); + }); + }); + + describe('BadRequest', () => { + it('should return the correct value', () => { + const result = HttpStatusCode.BadRequest; + expect(result).to.equal(400); + }); + }); + + describe('Unauthorized', () => { + it('should return the correct value', () => { + const result = HttpStatusCode.Unauthorized; + expect(result).to.equal(401); + }); + }); + + describe('Forbidden', () => { + it('should return the correct value', () => { + const result = HttpStatusCode.Forbidden; + expect(result).to.equal(403); + }); + }); + + describe('NotFound', () => { + it('should return the correct value', () => { + const result = HttpStatusCode.NotFound; + expect(result).to.equal(404); + }); + }); + + describe('MethodNotAllowed', () => { + it('should return the correct value', () => { + const result = HttpStatusCode.MethodNotAllowed; + expect(result).to.equal(405); + }); + }); + + describe('Conflict', () => { + it('should return the correct value', () => { + const result = HttpStatusCode.Conflict; + expect(result).to.equal(409); + }); + }); + + describe('InternalServerError', () => { + it('should return the correct value', () => { + const result = HttpStatusCode.InternalServerError; + expect(result).to.equal(500); + }); + }); + + describe('NotImplemented', () => { + it('should return the correct value', () => { + const result = HttpStatusCode.NotImplemented; + expect(result).to.equal(501); + }); + }); + + describe('BadGateway', () => { + it('should return the correct value', () => { + const result = HttpStatusCode.BadGateway; + expect(result).to.equal(502); + }); + }); + + describe('ServiceUnavailable', () => { + it('should return the correct value', () => { + const result = HttpStatusCode.ServiceUnavailable; + expect(result).to.equal(503); + }); + }); + + describe('GatewayTimeout', () => { + it('should return the correct value', () => { + const result = HttpStatusCode.GatewayTimeout; + expect(result).to.equal(504); + }); + }); +}); \ No newline at end of file