You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

client.spec.ts 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /// <reference path='typings/globals.d.ts' />
  2. import Client, {
  3. Config,
  4. RawResponse,
  5. Authentication,
  6. ClientFn,
  7. Response,
  8. rawResponseSymbol,
  9. } from '../src/client'
  10. const mockRawResponse: RawResponse = {
  11. body: 'any',
  12. headers: { some: 'header' },
  13. statusCode: 200,
  14. statusMessage: 'OK',
  15. }
  16. const mockTransformedResponse: Response = {
  17. status: {
  18. code: 200,
  19. word: 'OK',
  20. },
  21. body: 'any',
  22. headers: { some: 'header' },
  23. [rawResponseSymbol]: mockRawResponse,
  24. }
  25. const mockConfig: Config = {
  26. baseUrl: 'https://dummy.domain',
  27. }
  28. const mockBearerAuthConfig: Authentication = {
  29. type: 'bearer',
  30. authToken: 'some-token-value'
  31. }
  32. const mockBasicAuthConfig: Authentication = {
  33. type: 'basic',
  34. username: 'some-username',
  35. password: 'some-password',
  36. }
  37. const mockAuthConfigNotSupported /*: Authentication*/ = {
  38. type: 'invalid type'
  39. }
  40. describe('Client', () => {
  41. let rawClient: ClientFn
  42. let client: Client
  43. describe('Construct request options', () => {
  44. before(() => {
  45. rawClient = sinon.spy()
  46. client = new Client(rawClient, mockConfig)
  47. })
  48. it('transforms request parameters with all defaults', () => {
  49. const requestOptions = client.constructRequestOptions('GET', '/some-path', {}, {})
  50. expect(requestOptions).to.deep.equal({
  51. baseUrl: "https://dummy.domain",
  52. body: {},
  53. headers: {},
  54. json: false,
  55. method: "GET",
  56. retries: 0,
  57. throwHttpErrors: true,
  58. timeout: 30000,
  59. url: "/some-path",
  60. })
  61. })
  62. it('adds additional headers from options', () => {
  63. const requestOptions = client.constructRequestOptions('GET', '/some-path', {}, {
  64. headers: {
  65. some: 'mock'
  66. }
  67. })
  68. expect(requestOptions).to.deep.equal({
  69. baseUrl: "https://dummy.domain",
  70. body: {},
  71. headers: { some: 'mock' },
  72. json: false,
  73. method: "GET",
  74. retries: 0,
  75. throwHttpErrors: true,
  76. timeout: 30000,
  77. url: "/some-path",
  78. })
  79. })
  80. describe('Authentication setup', () => {
  81. before(() => {
  82. rawClient = sinon.spy()
  83. client = new Client(rawClient, mockConfig)
  84. })
  85. it('adds basic authentication', () => {
  86. // TODO:
  87. })
  88. it('adds bearer authentication', () => {
  89. // TODO:
  90. })
  91. })
  92. })
  93. describe('Setup authentication', () => {
  94. before(() => {
  95. rawClient = sinon.spy()
  96. client = new Client(rawClient, mockConfig)
  97. })
  98. it('throws for non-supported types', () => {
  99. // TODO: unable to test it as Typescript won't allow me to even compile it
  100. // expect(() => client.setupAuthentication(mockAuthConfigNotSupported))
  101. // .to.throw()
  102. })
  103. it('transforms authentication spec for basic auth', () => {
  104. expect(client.setupAuthentication(mockBasicAuthConfig))
  105. .to.deep.equal({
  106. key: 'Authorization',
  107. useHeader: true,
  108. usePayload: false,
  109. useQueryString: false,
  110. value: 'Basic c29tZS11c2VybmFtZTpzb21lLXBhc3N3b3Jk',
  111. })
  112. })
  113. it('transforms authentication spec for bearer auth', () => {
  114. expect(client.setupAuthentication(mockBearerAuthConfig))
  115. .to.deep.equal({
  116. key: 'Authorization',
  117. useHeader: true,
  118. usePayload: false,
  119. useQueryString: false,
  120. value: 'Bearer some-token-value',
  121. })
  122. })
  123. })
  124. it('transform raw response', () => {
  125. client = new Client(sinon.spy(), mockConfig)
  126. expect(client.transformResponse(mockRawResponse))
  127. .to.deep.equal(mockTransformedResponse)
  128. })
  129. })