@api-client/core 0.18.11 → 0.18.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (264) hide show
  1. package/build/src/browser.d.ts +0 -3
  2. package/build/src/browser.d.ts.map +1 -1
  3. package/build/src/browser.js +0 -3
  4. package/build/src/browser.js.map +1 -1
  5. package/build/src/index.d.ts +2 -5
  6. package/build/src/index.d.ts.map +1 -1
  7. package/build/src/index.js +2 -5
  8. package/build/src/index.js.map +1 -1
  9. package/build/src/lib/logging/DefaultLogger.d.ts +14 -0
  10. package/build/src/lib/logging/DefaultLogger.d.ts.map +1 -1
  11. package/build/src/lib/logging/DefaultLogger.js +27 -0
  12. package/build/src/lib/logging/DefaultLogger.js.map +1 -1
  13. package/build/src/lib/logging/index.d.ts +4 -0
  14. package/build/src/lib/logging/index.d.ts.map +1 -0
  15. package/build/src/lib/logging/index.js +10 -0
  16. package/build/src/lib/logging/index.js.map +1 -0
  17. package/build/src/modeling/DomainModel.d.ts.map +1 -1
  18. package/build/src/modeling/DomainModel.js +11 -4
  19. package/build/src/modeling/DomainModel.js.map +1 -1
  20. package/build/src/models/ClientCertificate.d.ts +1 -1
  21. package/build/src/models/ClientCertificate.js.map +1 -1
  22. package/build/src/models/RequestConfig.d.ts +1 -1
  23. package/build/src/models/RequestConfig.js.map +1 -1
  24. package/build/src/models/SerializableError.d.ts +1 -1
  25. package/build/src/models/SerializableError.d.ts.map +1 -1
  26. package/build/src/models/SerializableError.js.map +1 -1
  27. package/build/src/proxy/RequestProxy.d.ts.map +1 -1
  28. package/build/src/proxy/RequestProxy.js +2 -2
  29. package/build/src/proxy/RequestProxy.js.map +1 -1
  30. package/build/src/runtime/http-engine/CoreEngine.d.ts +218 -139
  31. package/build/src/runtime/http-engine/CoreEngine.d.ts.map +1 -1
  32. package/build/src/runtime/http-engine/CoreEngine.js +716 -870
  33. package/build/src/runtime/http-engine/CoreEngine.js.map +1 -1
  34. package/build/src/runtime/http-engine/PayloadSupport.d.ts.map +1 -1
  35. package/build/src/runtime/http-engine/PayloadSupport.js +2 -1
  36. package/build/src/runtime/http-engine/PayloadSupport.js.map +1 -1
  37. package/build/src/runtime/http-engine/auth/AuthManager.d.ts +73 -0
  38. package/build/src/runtime/http-engine/auth/AuthManager.d.ts.map +1 -0
  39. package/build/src/runtime/http-engine/auth/AuthManager.js +186 -0
  40. package/build/src/runtime/http-engine/auth/AuthManager.js.map +1 -0
  41. package/build/src/runtime/http-engine/auth/index.d.ts +2 -0
  42. package/build/src/runtime/http-engine/auth/index.d.ts.map +1 -0
  43. package/build/src/runtime/http-engine/auth/index.js +2 -0
  44. package/build/src/runtime/http-engine/auth/index.js.map +1 -0
  45. package/build/src/runtime/http-engine/certificates/CertificateManager.d.ts +11 -0
  46. package/build/src/runtime/http-engine/certificates/CertificateManager.d.ts.map +1 -0
  47. package/build/src/runtime/http-engine/certificates/CertificateManager.js +76 -0
  48. package/build/src/runtime/http-engine/certificates/CertificateManager.js.map +1 -0
  49. package/build/src/runtime/http-engine/certificates/index.d.ts +2 -0
  50. package/build/src/runtime/http-engine/certificates/index.d.ts.map +1 -0
  51. package/build/src/runtime/http-engine/certificates/index.js +2 -0
  52. package/build/src/runtime/http-engine/certificates/index.js.map +1 -0
  53. package/build/src/runtime/http-engine/compression/CompressionManager.d.ts +25 -0
  54. package/build/src/runtime/http-engine/compression/CompressionManager.d.ts.map +1 -0
  55. package/build/src/runtime/http-engine/compression/CompressionManager.js +89 -0
  56. package/build/src/runtime/http-engine/compression/CompressionManager.js.map +1 -0
  57. package/build/src/runtime/http-engine/compression/index.d.ts +2 -0
  58. package/build/src/runtime/http-engine/compression/index.d.ts.map +1 -0
  59. package/build/src/runtime/http-engine/compression/index.js +2 -0
  60. package/build/src/runtime/http-engine/compression/index.js.map +1 -0
  61. package/build/src/runtime/http-engine/connections/ConnectionManager.d.ts +57 -0
  62. package/build/src/runtime/http-engine/connections/ConnectionManager.d.ts.map +1 -0
  63. package/build/src/runtime/http-engine/connections/ConnectionManager.js +78 -0
  64. package/build/src/runtime/http-engine/connections/ConnectionManager.js.map +1 -0
  65. package/build/src/runtime/http-engine/connections/DigestAuthHandler.d.ts +70 -0
  66. package/build/src/runtime/http-engine/connections/DigestAuthHandler.d.ts.map +1 -0
  67. package/build/src/runtime/http-engine/connections/DigestAuthHandler.js +184 -0
  68. package/build/src/runtime/http-engine/connections/DigestAuthHandler.js.map +1 -0
  69. package/build/src/runtime/http-engine/connections/DirectConnection.d.ts +22 -0
  70. package/build/src/runtime/http-engine/connections/DirectConnection.d.ts.map +1 -0
  71. package/build/src/runtime/http-engine/connections/DirectConnection.js +105 -0
  72. package/build/src/runtime/http-engine/connections/DirectConnection.js.map +1 -0
  73. package/build/src/runtime/http-engine/connections/ProxyAuthHandler.d.ts +60 -0
  74. package/build/src/runtime/http-engine/connections/ProxyAuthHandler.d.ts.map +1 -0
  75. package/build/src/runtime/http-engine/connections/ProxyAuthHandler.js +138 -0
  76. package/build/src/runtime/http-engine/connections/ProxyAuthHandler.js.map +1 -0
  77. package/build/src/runtime/http-engine/connections/ProxyConnection.d.ts +14 -0
  78. package/build/src/runtime/http-engine/connections/ProxyConnection.d.ts.map +1 -0
  79. package/build/src/runtime/http-engine/connections/ProxyConnection.js +47 -0
  80. package/build/src/runtime/http-engine/connections/ProxyConnection.js.map +1 -0
  81. package/build/src/runtime/http-engine/connections/TunnelConnection.d.ts +13 -0
  82. package/build/src/runtime/http-engine/connections/TunnelConnection.d.ts.map +1 -0
  83. package/build/src/runtime/http-engine/connections/TunnelConnection.js +175 -0
  84. package/build/src/runtime/http-engine/connections/TunnelConnection.js.map +1 -0
  85. package/build/src/runtime/http-engine/connections/index.d.ts +7 -0
  86. package/build/src/runtime/http-engine/connections/index.d.ts.map +1 -0
  87. package/build/src/runtime/http-engine/connections/index.js +7 -0
  88. package/build/src/runtime/http-engine/connections/index.js.map +1 -0
  89. package/build/src/runtime/http-engine/constants.d.ts +69 -0
  90. package/build/src/runtime/http-engine/constants.d.ts.map +1 -0
  91. package/build/src/runtime/http-engine/constants.js +90 -0
  92. package/build/src/runtime/http-engine/constants.js.map +1 -0
  93. package/build/src/runtime/http-engine/cookies/CookieProcessor.d.ts +5 -0
  94. package/build/src/runtime/http-engine/cookies/CookieProcessor.d.ts.map +1 -0
  95. package/build/src/runtime/http-engine/cookies/CookieProcessor.js +20 -0
  96. package/build/src/runtime/http-engine/cookies/CookieProcessor.js.map +1 -0
  97. package/build/src/runtime/http-engine/cookies/index.d.ts +2 -0
  98. package/build/src/runtime/http-engine/cookies/index.d.ts.map +1 -0
  99. package/build/src/runtime/http-engine/cookies/index.js +2 -0
  100. package/build/src/runtime/http-engine/cookies/index.js.map +1 -0
  101. package/build/src/runtime/http-engine/errors/HttpEngineErrors.d.ts +156 -0
  102. package/build/src/runtime/http-engine/errors/HttpEngineErrors.d.ts.map +1 -0
  103. package/build/src/runtime/http-engine/errors/HttpEngineErrors.js +227 -0
  104. package/build/src/runtime/http-engine/errors/HttpEngineErrors.js.map +1 -0
  105. package/build/src/runtime/http-engine/errors/index.d.ts +2 -0
  106. package/build/src/runtime/http-engine/errors/index.d.ts.map +1 -0
  107. package/build/src/runtime/http-engine/errors/index.js +2 -0
  108. package/build/src/runtime/http-engine/errors/index.js.map +1 -0
  109. package/build/src/runtime/http-engine/message/MessageBuilder.d.ts +66 -0
  110. package/build/src/runtime/http-engine/message/MessageBuilder.d.ts.map +1 -0
  111. package/build/src/runtime/http-engine/message/MessageBuilder.js +161 -0
  112. package/build/src/runtime/http-engine/message/MessageBuilder.js.map +1 -0
  113. package/build/src/runtime/http-engine/message/MessageProcessor.d.ts +27 -0
  114. package/build/src/runtime/http-engine/message/MessageProcessor.d.ts.map +1 -0
  115. package/build/src/runtime/http-engine/message/MessageProcessor.js +51 -0
  116. package/build/src/runtime/http-engine/message/MessageProcessor.js.map +1 -0
  117. package/build/src/runtime/http-engine/message/index.d.ts +3 -0
  118. package/build/src/runtime/http-engine/message/index.d.ts.map +1 -0
  119. package/build/src/runtime/http-engine/message/index.js +3 -0
  120. package/build/src/runtime/http-engine/message/index.js.map +1 -0
  121. package/build/src/runtime/http-engine/ntlm/NtlmAuth.d.ts +2 -8
  122. package/build/src/runtime/http-engine/ntlm/NtlmAuth.d.ts.map +1 -1
  123. package/build/src/runtime/http-engine/ntlm/NtlmAuth.js +11 -5
  124. package/build/src/runtime/http-engine/ntlm/NtlmAuth.js.map +1 -1
  125. package/build/src/runtime/http-engine/ntlm/NtlmMessage.js +6 -6
  126. package/build/src/runtime/http-engine/ntlm/NtlmMessage.js.map +1 -1
  127. package/build/src/runtime/http-engine/parsers/BodyParser.d.ts +39 -0
  128. package/build/src/runtime/http-engine/parsers/BodyParser.d.ts.map +1 -0
  129. package/build/src/runtime/http-engine/parsers/BodyParser.js +145 -0
  130. package/build/src/runtime/http-engine/parsers/BodyParser.js.map +1 -0
  131. package/build/src/runtime/http-engine/parsers/HeadersParser.d.ts +29 -0
  132. package/build/src/runtime/http-engine/parsers/HeadersParser.d.ts.map +1 -0
  133. package/build/src/runtime/http-engine/parsers/HeadersParser.js +88 -0
  134. package/build/src/runtime/http-engine/parsers/HeadersParser.js.map +1 -0
  135. package/build/src/runtime/http-engine/parsers/HttpResponseParser.d.ts +91 -0
  136. package/build/src/runtime/http-engine/parsers/HttpResponseParser.d.ts.map +1 -0
  137. package/build/src/runtime/http-engine/parsers/HttpResponseParser.js +236 -0
  138. package/build/src/runtime/http-engine/parsers/HttpResponseParser.js.map +1 -0
  139. package/build/src/runtime/http-engine/parsers/StatusParser.d.ts +20 -0
  140. package/build/src/runtime/http-engine/parsers/StatusParser.d.ts.map +1 -0
  141. package/build/src/runtime/http-engine/parsers/StatusParser.js +51 -0
  142. package/build/src/runtime/http-engine/parsers/StatusParser.js.map +1 -0
  143. package/build/src/runtime/http-engine/parsers/index.d.ts +5 -0
  144. package/build/src/runtime/http-engine/parsers/index.d.ts.map +1 -0
  145. package/build/src/runtime/http-engine/parsers/index.js +5 -0
  146. package/build/src/runtime/http-engine/parsers/index.js.map +1 -0
  147. package/build/src/runtime/http-engine/response/ResponseProcessor.d.ts +22 -0
  148. package/build/src/runtime/http-engine/response/ResponseProcessor.d.ts.map +1 -0
  149. package/build/src/runtime/http-engine/response/ResponseProcessor.js +25 -0
  150. package/build/src/runtime/http-engine/response/ResponseProcessor.js.map +1 -0
  151. package/build/src/runtime/http-engine/response/index.d.ts +2 -0
  152. package/build/src/runtime/http-engine/response/index.d.ts.map +1 -0
  153. package/build/src/runtime/http-engine/response/index.js +2 -0
  154. package/build/src/runtime/http-engine/response/index.js.map +1 -0
  155. package/build/src/runtime/http-engine/statistics/StatisticsProcessor.d.ts +7 -0
  156. package/build/src/runtime/http-engine/statistics/StatisticsProcessor.d.ts.map +1 -0
  157. package/build/src/runtime/http-engine/statistics/StatisticsProcessor.js +40 -0
  158. package/build/src/runtime/http-engine/statistics/StatisticsProcessor.js.map +1 -0
  159. package/build/src/runtime/http-engine/statistics/index.d.ts +2 -0
  160. package/build/src/runtime/http-engine/statistics/index.d.ts.map +1 -0
  161. package/build/src/runtime/http-engine/statistics/index.js +2 -0
  162. package/build/src/runtime/http-engine/statistics/index.js.map +1 -0
  163. package/build/src/runtime/http-engine/url/UrlProcessor.d.ts +24 -0
  164. package/build/src/runtime/http-engine/url/UrlProcessor.d.ts.map +1 -0
  165. package/build/src/runtime/http-engine/url/UrlProcessor.js +50 -0
  166. package/build/src/runtime/http-engine/url/UrlProcessor.js.map +1 -0
  167. package/build/src/runtime/http-engine/url/index.d.ts +2 -0
  168. package/build/src/runtime/http-engine/url/index.d.ts.map +1 -0
  169. package/build/src/runtime/http-engine/url/index.js +2 -0
  170. package/build/src/runtime/http-engine/url/index.js.map +1 -0
  171. package/build/src/runtime/http-runner/HttpRequestRunner.d.ts +3 -3
  172. package/build/src/runtime/http-runner/HttpRequestRunner.d.ts.map +1 -1
  173. package/build/src/runtime/http-runner/HttpRequestRunner.js.map +1 -1
  174. package/build/src/runtime/node/InteropInterfaces.d.ts +3 -3
  175. package/build/src/runtime/node/InteropInterfaces.d.ts.map +1 -1
  176. package/build/src/runtime/node/InteropInterfaces.js.map +1 -1
  177. package/build/src/runtime/node/ProjectRequestRunner.d.ts +2 -2
  178. package/build/src/runtime/node/ProjectRequestRunner.d.ts.map +1 -1
  179. package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -1
  180. package/build/src/runtime/node/ProjectRunner.d.ts.map +1 -1
  181. package/build/src/runtime/node/ProjectRunner.js +2 -2
  182. package/build/src/runtime/node/ProjectRunner.js.map +1 -1
  183. package/build/tsconfig.tsbuildinfo +1 -1
  184. package/data/models/example-generator-api.json +24 -24
  185. package/package.json +2 -2
  186. package/src/lib/logging/DefaultLogger.ts +32 -0
  187. package/src/modeling/DomainModel.ts +11 -4
  188. package/src/models/ClientCertificate.ts +1 -1
  189. package/src/models/RequestConfig.ts +1 -1
  190. package/src/models/SerializableError.ts +1 -1
  191. package/src/proxy/RequestProxy.ts +2 -2
  192. package/src/runtime/http-engine/CoreEngine.ts +858 -893
  193. package/src/runtime/http-engine/PayloadSupport.ts +2 -1
  194. package/src/runtime/http-engine/auth/AuthManager.ts +242 -0
  195. package/src/runtime/http-engine/certificates/CertificateManager.ts +74 -0
  196. package/src/runtime/http-engine/compression/CompressionManager.ts +99 -0
  197. package/src/runtime/http-engine/connections/ConnectionManager.ts +123 -0
  198. package/src/runtime/http-engine/connections/DigestAuthHandler.ts +238 -0
  199. package/src/runtime/http-engine/connections/DirectConnection.ts +134 -0
  200. package/src/runtime/http-engine/connections/ProxyAuthHandler.ts +179 -0
  201. package/src/runtime/http-engine/connections/ProxyConnection.ts +55 -0
  202. package/src/runtime/http-engine/connections/TunnelConnection.ts +192 -0
  203. package/src/runtime/http-engine/constants.ts +103 -0
  204. package/src/runtime/http-engine/cookies/CookieProcessor.ts +25 -0
  205. package/src/runtime/http-engine/errors/HttpEngineErrors.ts +319 -0
  206. package/src/runtime/http-engine/message/MessageBuilder.ts +201 -0
  207. package/src/runtime/http-engine/message/MessageProcessor.ts +73 -0
  208. package/src/runtime/http-engine/ntlm/NtlmAuth.ts +16 -13
  209. package/src/runtime/http-engine/ntlm/NtlmMessage.ts +6 -6
  210. package/src/runtime/http-engine/parsers/BodyParser.ts +171 -0
  211. package/src/runtime/http-engine/parsers/HeadersParser.ts +103 -0
  212. package/src/runtime/http-engine/parsers/HttpResponseParser.ts +280 -0
  213. package/src/runtime/http-engine/parsers/StatusParser.ts +69 -0
  214. package/src/runtime/http-engine/response/ResponseProcessor.ts +46 -0
  215. package/src/runtime/http-engine/statistics/StatisticsProcessor.ts +52 -0
  216. package/src/runtime/http-engine/url/UrlProcessor.ts +55 -0
  217. package/src/runtime/http-runner/HttpRequestRunner.ts +3 -3
  218. package/src/runtime/node/InteropInterfaces.ts +3 -3
  219. package/src/runtime/node/ProjectRequestRunner.ts +2 -2
  220. package/src/runtime/node/ProjectRunner.ts +2 -2
  221. package/tests/servers/ProxyServer.ts +32 -19
  222. package/tests/servers/express-routes/ApiEndpoint.ts +24 -0
  223. package/tests/servers/express-routes/BasicAuthRoute.ts +36 -0
  224. package/tests/servers/express-routes/BearerAuthRoute.ts +35 -0
  225. package/tests/servers/express-routes/NTLMRoute.ts +2 -3
  226. package/tests/servers/express-routes/PostApi.ts +15 -2
  227. package/tests/servers/express-routes/RedirectsApi.ts +12 -1
  228. package/tests/servers/express-routes/ResponsesApi.ts +1 -1
  229. package/tests/servers/express-routes/StreamApi.ts +19 -0
  230. package/tests/servers/oauth2mock/ServerMock.js +1 -1
  231. package/tests/unit/modeling/domain_model_entities.spec.ts +306 -1
  232. package/tests/unit/runtime/http-engine/HttpResponseParser.spec.ts +337 -0
  233. package/tests/unit/runtime/http-engine/abort.spec.ts +4 -5
  234. package/tests/unit/runtime/http-engine/auth.spec.ts +7 -58
  235. package/tests/unit/runtime/http-engine/certificates/CertificateManager.spec.ts +482 -0
  236. package/tests/unit/runtime/http-engine/certificates.spec.ts +2 -2
  237. package/tests/unit/runtime/http-engine/compression/CompressionManager.spec.ts +498 -0
  238. package/tests/unit/runtime/http-engine/compression.spec.ts +3 -72
  239. package/tests/unit/runtime/http-engine/connections/ConnectionManager.spec.ts +379 -0
  240. package/tests/unit/runtime/http-engine/connections/DigestAuthHandler.spec.ts +164 -0
  241. package/tests/unit/runtime/http-engine/core_engine.spec.ts +561 -0
  242. package/tests/unit/runtime/http-engine/engine_statuses.spec.ts +2 -2
  243. package/tests/unit/runtime/http-engine/events.spec.ts +2 -2
  244. package/tests/unit/runtime/http-engine/headers.spec.ts +2 -88
  245. package/tests/unit/runtime/http-engine/hosts.spec.ts +2 -2
  246. package/tests/unit/runtime/http-engine/http-get.spec.ts +2 -2
  247. package/tests/unit/runtime/http-engine/http-post.spec.ts +2 -2
  248. package/tests/unit/runtime/http-engine/logger.spec.ts +0 -8
  249. package/tests/unit/runtime/http-engine/message.spec.ts +2 -194
  250. package/tests/unit/runtime/http-engine/params.spec.ts +4 -4
  251. package/tests/unit/runtime/http-engine/proxy.spec.ts +15 -14
  252. package/tests/unit/runtime/http-engine/redirects.spec.ts +2 -2
  253. package/tests/unit/runtime/http-engine/responses.spec.ts +170 -277
  254. package/tests/unit/runtime/http-engine/timeout.spec.ts +3 -3
  255. package/tests/unit/runtime/http-engine/timings.spec.ts +2 -2
  256. package/tests/unit/runtime/proxy/HttpProjectProxy.spec.ts +25 -28
  257. package/tests/unit/runtime/runners/project_runner.spec.ts +2 -2
  258. package/tests/unit/runtime/runners/request_runner.spec.ts +2 -2
  259. package/build/src/runtime/http-engine/HttpEngine.d.ts +0 -311
  260. package/build/src/runtime/http-engine/HttpEngine.d.ts.map +0 -1
  261. package/build/src/runtime/http-engine/HttpEngine.js +0 -802
  262. package/build/src/runtime/http-engine/HttpEngine.js.map +0 -1
  263. package/src/runtime/http-engine/HttpEngine.ts +0 -952
  264. package/tests/unit/runtime/http-engine/connecting.spec.ts +0 -140
@@ -1,16 +1,7 @@
1
1
  import { test } from '@japa/runner'
2
- import {
3
- CoreEngine,
4
- DummyLogger,
5
- HttpEngineOptions,
6
- IHttpRequest,
7
- INtlmAuthorization,
8
- IResponse,
9
- RequestAuthorizationKind,
10
- Headers,
11
- } from '../../../../src/index.js'
2
+ import { CoreEngine, createLogger, IResponse, Response, RequestAuthorizationKind } from '../../../../src/index.js'
12
3
 
13
- const logger = new DummyLogger()
4
+ const logger = createLogger()
14
5
 
15
6
  test.group('NTLM authorization', () => {
16
7
  test('returns 401 when no credentials', async ({ assert, httpConfig }) => {
@@ -50,53 +41,11 @@ test.group('NTLM authorization', () => {
50
41
  }
51
42
  )
52
43
  const log = await engine.send()
53
- const response = log.response as IResponse
44
+ const response = new Response(log.response as IResponse)
54
45
  assert.equal(response.status, 200, 'has the 200 status code')
55
- }).tags(['@http-engine', '@auth'])
56
- })
57
-
58
- test.group('_authorizeNtlm()', (group) => {
59
- let headers: Headers
60
- let engine: CoreEngine
61
- let request: IHttpRequest
62
- let opts: HttpEngineOptions
63
- let authConfig: INtlmAuthorization
64
-
65
- group.each.setup((ctx) => {
66
- headers = new Headers()
67
- request = {
68
- url: `http://localhost:${ctx.context.httpConfig.httpPort}/v1/get/`,
69
- method: 'GET',
70
- headers: 'Host: test.com',
71
- }
72
- authConfig = {
73
- domain: 'domain.com',
74
- username: 'test',
75
- password: 'test',
76
- }
77
- opts = {
78
- logger,
79
- authorization: [
80
- {
81
- kind: RequestAuthorizationKind,
82
- type: 'ntlm',
83
- enabled: true,
84
- valid: true,
85
- config: authConfig,
86
- },
87
- ],
88
- }
89
- engine = new CoreEngine(request, opts)
90
- })
91
-
92
- test('adds the authorization header', ({ assert }) => {
93
- engine._authorizeNtlm(authConfig, headers)
94
- assert.isTrue(headers.has('Authorization'))
95
- }).tags(['@http-engine', '@auth'])
96
-
97
- test('the authorization is NTLM', ({ assert }) => {
98
- engine._authorizeNtlm(authConfig as INtlmAuthorization, headers)
99
- const value = headers.get('Authorization') as string
100
- assert.equal(value.indexOf('NTLM '), 0)
46
+ const body = await response.readPayloadAsString()
47
+ assert.ok(body, 'has the body')
48
+ const rsp = JSON.parse(body as string)
49
+ assert.deepEqual(rsp, { authenticated: true }, 'has the authenticated response')
101
50
  }).tags(['@http-engine', '@auth'])
102
51
  })
@@ -0,0 +1,482 @@
1
+ import { test } from '@japa/runner'
2
+ import tls from 'tls'
3
+ import sinon from 'sinon'
4
+ import {
5
+ addClientCertificate,
6
+ checkServerIdentity,
7
+ } from '../../../../../src/runtime/http-engine/certificates/CertificateManager.js'
8
+ import { HttpCertificate, IPemCertificate, IP12Certificate } from '../../../../../src/models/ClientCertificate.js'
9
+
10
+ test.group('addClientCertificate()', (group) => {
11
+ let options: tls.ConnectionOptions
12
+
13
+ group.each.setup(() => {
14
+ options = {}
15
+ })
16
+
17
+ test('returns early when certificate is null', ({ assert }) => {
18
+ const originalOptions = { ...options }
19
+ addClientCertificate(null as unknown as HttpCertificate, options)
20
+ assert.deepEqual(options, originalOptions, 'options should not be modified')
21
+ })
22
+
23
+ test('returns early when certificate is undefined', ({ assert }) => {
24
+ const originalOptions = { ...options }
25
+ addClientCertificate(undefined as unknown as HttpCertificate, options)
26
+ assert.deepEqual(options, originalOptions, 'options should not be modified')
27
+ })
28
+
29
+ test('adds p12 certificate to empty pfx array', ({ assert }) => {
30
+ const cert: IP12Certificate = {
31
+ kind: 'Core#Certificate',
32
+ key: 'test-key',
33
+ name: 'Test P12',
34
+ type: 'p12',
35
+ cert: {
36
+ data: Buffer.from('test-cert-data'),
37
+ },
38
+ }
39
+
40
+ addClientCertificate(cert, options)
41
+
42
+ assert.isArray(options.pfx, 'pfx should be an array')
43
+ assert.lengthOf(options.pfx as tls.PxfObject[], 1, 'pfx should contain one certificate')
44
+
45
+ const pfxEntry = (options.pfx as tls.PxfObject[])[0]
46
+ assert.deepEqual(pfxEntry.buf, Buffer.from('test-cert-data'), 'certificate data should match')
47
+ assert.isUndefined(pfxEntry.passphrase, 'passphrase should not be set when not provided')
48
+ })
49
+
50
+ test('adds p12 certificate with passphrase', ({ assert }) => {
51
+ const cert: IP12Certificate = {
52
+ kind: 'Core#Certificate',
53
+ key: 'test-key',
54
+ name: 'Test P12 with passphrase',
55
+ type: 'p12',
56
+ cert: {
57
+ data: Buffer.from('test-cert-data'),
58
+ passphrase: 'secret-passphrase',
59
+ },
60
+ }
61
+
62
+ addClientCertificate(cert, options)
63
+
64
+ const pfxEntry = (options.pfx as tls.PxfObject[])[0]
65
+ assert.equal(pfxEntry.passphrase, 'secret-passphrase', 'passphrase should be set')
66
+ })
67
+
68
+ test('converts existing pfx non-array to array and adds new certificate', ({ assert }) => {
69
+ const existingPfx: tls.PxfObject = {
70
+ buf: Buffer.from('existing-cert'),
71
+ }
72
+ options.pfx = existingPfx as unknown as (string | Buffer | tls.PxfObject)[]
73
+
74
+ const cert: IP12Certificate = {
75
+ kind: 'Core#Certificate',
76
+ key: 'test-key',
77
+ name: 'Test P12',
78
+ type: 'p12',
79
+ cert: {
80
+ data: Buffer.from('new-cert-data'),
81
+ },
82
+ }
83
+
84
+ addClientCertificate(cert, options)
85
+
86
+ assert.isArray(options.pfx, 'pfx should be converted to array')
87
+ assert.lengthOf(options.pfx as tls.PxfObject[], 2, 'pfx should contain both certificates')
88
+ assert.deepEqual((options.pfx as tls.PxfObject[])[0], existingPfx, 'existing certificate should be preserved')
89
+ assert.deepEqual(
90
+ (options.pfx as tls.PxfObject[])[1].buf,
91
+ Buffer.from('new-cert-data'),
92
+ 'new certificate should be added'
93
+ )
94
+ })
95
+
96
+ test('converts existing pfx non-array to array and adds new certificate', ({ assert }) => {
97
+ const existingPfx: tls.PxfObject = {
98
+ buf: Buffer.from('existing-cert'),
99
+ }
100
+ options.pfx = existingPfx as unknown as (string | Buffer | tls.PxfObject)[]
101
+
102
+ const cert: IP12Certificate = {
103
+ kind: 'Core#Certificate',
104
+ key: 'test-key',
105
+ name: 'Test P12',
106
+ type: 'p12',
107
+ cert: {
108
+ data: Buffer.from('new-cert-data'),
109
+ },
110
+ }
111
+
112
+ addClientCertificate(cert, options)
113
+
114
+ assert.isArray(options.pfx, 'pfx should be converted to array')
115
+ assert.lengthOf(options.pfx as tls.PxfObject[], 2, 'pfx should contain both certificates')
116
+ assert.deepEqual((options.pfx as tls.PxfObject[])[0], existingPfx, 'existing certificate should be preserved')
117
+ assert.deepEqual(
118
+ (options.pfx as tls.PxfObject[])[1].buf,
119
+ Buffer.from('new-cert-data'),
120
+ 'new certificate should be added'
121
+ )
122
+ })
123
+
124
+ test('adds certificate to existing pfx array', ({ assert }) => {
125
+ const existingPfx: tls.PxfObject = {
126
+ buf: Buffer.from('existing-cert'),
127
+ }
128
+ options.pfx = [existingPfx]
129
+
130
+ const cert: IP12Certificate = {
131
+ kind: 'Core#Certificate',
132
+ key: 'test-key',
133
+ name: 'Test P12',
134
+ type: 'p12',
135
+ cert: {
136
+ data: Buffer.from('new-cert-data'),
137
+ },
138
+ }
139
+
140
+ addClientCertificate(cert, options)
141
+
142
+ assert.lengthOf(options.pfx as tls.PxfObject[], 2, 'pfx should contain both certificates')
143
+ assert.deepEqual((options.pfx as tls.PxfObject[])[0], existingPfx, 'existing certificate should be preserved')
144
+ assert.deepEqual(
145
+ (options.pfx as tls.PxfObject[])[1].buf,
146
+ Buffer.from('new-cert-data'),
147
+ 'new certificate should be added'
148
+ )
149
+ })
150
+
151
+ test('handles Buffer data correctly for p12', ({ assert }) => {
152
+ const certData = Buffer.from('test-buffer-data')
153
+ const cert: IP12Certificate = {
154
+ kind: 'Core#Certificate',
155
+ key: 'test-key',
156
+ name: 'Test P12 Buffer',
157
+ type: 'p12',
158
+ cert: {
159
+ data: certData,
160
+ },
161
+ }
162
+
163
+ addClientCertificate(cert, options)
164
+
165
+ const pfxEntry = (options.pfx as tls.PxfObject[])[0]
166
+ assert.deepEqual(pfxEntry.buf, certData, 'buffer data should be preserved')
167
+ })
168
+
169
+ test('adds pem certificate without key to empty cert array', ({ assert }) => {
170
+ const cert: IPemCertificate = {
171
+ kind: 'Core#Certificate',
172
+ key: 'test-key',
173
+ name: 'Test PEM',
174
+ type: 'pem',
175
+ cert: {
176
+ data: Buffer.from('test-cert-data'),
177
+ },
178
+ certKey: {
179
+ data: Buffer.from('test-key-data'),
180
+ },
181
+ }
182
+
183
+ addClientCertificate(cert, options)
184
+
185
+ assert.isArray(options.cert, 'cert should be an array')
186
+ assert.lengthOf(options.cert as Buffer[], 1, 'cert should contain one certificate')
187
+ assert.deepEqual((options.cert as Buffer[])[0], Buffer.from('test-cert-data'), 'certificate data should match')
188
+ })
189
+
190
+ test('adds pem certificate with key', ({ assert }) => {
191
+ const cert: IPemCertificate = {
192
+ kind: 'Core#Certificate',
193
+ key: 'test-key',
194
+ name: 'Test PEM with key',
195
+ type: 'pem',
196
+ cert: {
197
+ data: Buffer.from('test-cert-data'),
198
+ },
199
+ certKey: {
200
+ data: Buffer.from('test-key-data'),
201
+ },
202
+ }
203
+
204
+ addClientCertificate(cert, options)
205
+
206
+ assert.isArray(options.cert, 'cert should be an array')
207
+ assert.isArray(options.key, 'key should be an array')
208
+ assert.lengthOf(options.cert as Buffer[], 1, 'cert should contain one certificate')
209
+ assert.lengthOf(options.key as tls.KeyObject[], 1, 'key should contain one key')
210
+
211
+ assert.deepEqual((options.cert as Buffer[])[0], Buffer.from('test-cert-data'), 'certificate data should match')
212
+
213
+ const keyEntry = (options.key as tls.KeyObject[])[0]
214
+ assert.deepEqual(keyEntry.pem, Buffer.from('test-key-data'), 'key data should match')
215
+ assert.isUndefined(keyEntry.passphrase, 'passphrase should not be set when not provided')
216
+ })
217
+
218
+ test('adds pem certificate with key and passphrase', ({ assert }) => {
219
+ const cert: IPemCertificate = {
220
+ kind: 'Core#Certificate',
221
+ key: 'test-key',
222
+ name: 'Test PEM with key and passphrase',
223
+ type: 'pem',
224
+ cert: {
225
+ data: Buffer.from('test-cert-data'),
226
+ },
227
+ certKey: {
228
+ data: Buffer.from('test-key-data'),
229
+ passphrase: 'key-passphrase',
230
+ },
231
+ }
232
+
233
+ addClientCertificate(cert, options)
234
+
235
+ const keyEntry = (options.key as tls.KeyObject[])[0]
236
+ assert.equal(keyEntry.passphrase, 'key-passphrase', 'key passphrase should be set')
237
+ })
238
+
239
+ test('converts existing cert non-array to array and adds new certificate', ({ assert }) => {
240
+ const existingCert = Buffer.from('existing-cert')
241
+ options.cert = existingCert as string | Buffer | (string | Buffer)[]
242
+
243
+ const cert: IPemCertificate = {
244
+ kind: 'Core#Certificate',
245
+ key: 'test-key',
246
+ name: 'Test PEM',
247
+ type: 'pem',
248
+ cert: {
249
+ data: Buffer.from('new-cert-data'),
250
+ },
251
+ certKey: {
252
+ data: Buffer.from('new-key-data'),
253
+ },
254
+ }
255
+
256
+ addClientCertificate(cert, options)
257
+
258
+ assert.isArray(options.cert, 'cert should be converted to array')
259
+ assert.lengthOf(options.cert as Buffer[], 2, 'cert should contain both certificates')
260
+ assert.deepEqual((options.cert as Buffer[])[0], existingCert, 'existing certificate should be preserved')
261
+ assert.deepEqual((options.cert as Buffer[])[1], Buffer.from('new-cert-data'), 'new certificate should be added')
262
+ })
263
+
264
+ test('converts existing key non-array to array and adds new key', ({ assert }) => {
265
+ const existingKey: tls.KeyObject = {
266
+ pem: Buffer.from('existing-key'),
267
+ }
268
+ options.key = existingKey as unknown as (string | Buffer | tls.KeyObject)[]
269
+
270
+ const cert: IPemCertificate = {
271
+ kind: 'Core#Certificate',
272
+ key: 'test-key',
273
+ name: 'Test PEM',
274
+ type: 'pem',
275
+ cert: {
276
+ data: Buffer.from('new-cert-data'),
277
+ },
278
+ certKey: {
279
+ data: Buffer.from('new-key-data'),
280
+ },
281
+ }
282
+
283
+ addClientCertificate(cert, options)
284
+
285
+ assert.isArray(options.key, 'key should be converted to array')
286
+ assert.lengthOf(options.key as tls.KeyObject[], 2, 'key should contain both keys')
287
+ assert.deepEqual((options.key as tls.KeyObject[])[0], existingKey, 'existing key should be preserved')
288
+ assert.deepEqual((options.key as tls.KeyObject[])[1].pem, Buffer.from('new-key-data'), 'new key should be added')
289
+ })
290
+
291
+ test('adds certificate to existing cert array', ({ assert }) => {
292
+ const existingCert = Buffer.from('existing-cert')
293
+ options.cert = [existingCert]
294
+
295
+ const cert: IPemCertificate = {
296
+ kind: 'Core#Certificate',
297
+ key: 'test-key',
298
+ name: 'Test PEM',
299
+ type: 'pem',
300
+ cert: {
301
+ data: Buffer.from('new-cert-data'),
302
+ },
303
+ certKey: {
304
+ data: Buffer.from('new-key-data'),
305
+ },
306
+ }
307
+
308
+ addClientCertificate(cert, options)
309
+
310
+ assert.lengthOf(options.cert as Buffer[], 2, 'cert should contain both certificates')
311
+ assert.deepEqual((options.cert as Buffer[])[0], existingCert, 'existing certificate should be preserved')
312
+ assert.deepEqual((options.cert as Buffer[])[1], Buffer.from('new-cert-data'), 'new certificate should be added')
313
+ })
314
+
315
+ test('handles Uint8Array data correctly', ({ assert }) => {
316
+ const certData = new Uint8Array([1, 2, 3, 4, 5])
317
+ const keyData = new Uint8Array([6, 7, 8, 9, 10])
318
+
319
+ const cert: IPemCertificate = {
320
+ kind: 'Core#Certificate',
321
+ key: 'test-key',
322
+ name: 'Test PEM Uint8Array',
323
+ type: 'pem',
324
+ cert: {
325
+ data: certData,
326
+ },
327
+ certKey: {
328
+ data: keyData,
329
+ },
330
+ }
331
+
332
+ addClientCertificate(cert, options)
333
+
334
+ assert.deepEqual(
335
+ (options.cert as Buffer[])[0],
336
+ Buffer.from(certData),
337
+ 'Uint8Array cert data should be converted to Buffer'
338
+ )
339
+ assert.deepEqual(
340
+ (options.key as tls.KeyObject[])[0].pem,
341
+ Buffer.from(keyData),
342
+ 'Uint8Array key data should be converted to Buffer'
343
+ )
344
+ })
345
+
346
+ test('adds pem certificate without certKey', ({ assert }) => {
347
+ const cert: HttpCertificate = {
348
+ kind: 'Core#Certificate',
349
+ key: 'test-key',
350
+ name: 'Test PEM without key',
351
+ type: 'pem',
352
+ cert: {
353
+ data: Buffer.from('test-cert-data'),
354
+ },
355
+ }
356
+
357
+ addClientCertificate(cert, options)
358
+
359
+ assert.isArray(options.cert, 'cert should be an array')
360
+ assert.lengthOf(options.cert as Buffer[], 1, 'cert should contain one certificate')
361
+ assert.isUndefined(options.key, 'key should not be set when certKey is not provided')
362
+ })
363
+
364
+ test('does not mutate original certificate object', ({ assert }) => {
365
+ const originalCert: IP12Certificate = {
366
+ kind: 'Core#Certificate',
367
+ key: 'test-key',
368
+ name: 'Test P12',
369
+ type: 'p12',
370
+ cert: {
371
+ data: Buffer.from('test-cert-data'),
372
+ passphrase: 'original-passphrase',
373
+ },
374
+ }
375
+
376
+ const certCopy = structuredClone(originalCert)
377
+ addClientCertificate(originalCert, options)
378
+
379
+ assert.deepEqual(originalCert, certCopy, 'original certificate should not be mutated')
380
+ })
381
+ })
382
+
383
+ test.group('checkServerIdentity()', (group) => {
384
+ let mockCheckServerIdentity: sinon.SinonStub
385
+
386
+ group.setup(() => {
387
+ mockCheckServerIdentity = sinon.stub(tls, 'checkServerIdentity')
388
+ })
389
+
390
+ group.teardown(() => {
391
+ mockCheckServerIdentity.restore()
392
+ })
393
+
394
+ group.each.teardown(() => {
395
+ mockCheckServerIdentity.reset()
396
+ })
397
+
398
+ test('returns undefined when tls.checkServerIdentity returns undefined', ({ assert }) => {
399
+ const host = 'example.com'
400
+ const cert = { subject: { CN: 'example.com' } } as tls.PeerCertificate
401
+
402
+ mockCheckServerIdentity.returns(undefined)
403
+
404
+ const result = checkServerIdentity(host, cert)
405
+
406
+ assert.isUndefined(result, 'should return undefined when no error')
407
+ assert.isTrue(
408
+ mockCheckServerIdentity.calledOnceWith(host, cert),
409
+ 'should call tls.checkServerIdentity with correct arguments'
410
+ )
411
+ })
412
+
413
+ test('returns error when tls.checkServerIdentity returns an error', ({ assert }) => {
414
+ const host = 'example.com'
415
+ const cert = { subject: { CN: 'different.com' } } as tls.PeerCertificate
416
+ const expectedError = new Error("Hostname/IP does not match certificate's altnames")
417
+
418
+ mockCheckServerIdentity.returns(expectedError)
419
+
420
+ const result = checkServerIdentity(host, cert)
421
+
422
+ assert.equal(result, expectedError, 'should return the error from tls.checkServerIdentity')
423
+ assert.isTrue(
424
+ mockCheckServerIdentity.calledOnceWith(host, cert),
425
+ 'should call tls.checkServerIdentity with correct arguments'
426
+ )
427
+ })
428
+
429
+ test('passes through different error types', ({ assert }) => {
430
+ const host = 'example.com'
431
+ const cert = { subject: { CN: 'example.com' } } as tls.PeerCertificate
432
+ const expectedError = new TypeError('Invalid certificate format')
433
+
434
+ mockCheckServerIdentity.returns(expectedError)
435
+
436
+ const result = checkServerIdentity(host, cert)
437
+
438
+ assert.equal(result, expectedError, 'should return any error type from tls.checkServerIdentity')
439
+ })
440
+
441
+ test('handles empty host string', ({ assert }) => {
442
+ const host = ''
443
+ const cert = { subject: { CN: 'example.com' } } as tls.PeerCertificate
444
+ const expectedError = new Error('Invalid host')
445
+
446
+ mockCheckServerIdentity.returns(expectedError)
447
+
448
+ const result = checkServerIdentity(host, cert)
449
+
450
+ assert.equal(result, expectedError, 'should handle empty host string')
451
+ assert.isTrue(
452
+ mockCheckServerIdentity.calledOnceWith(host, cert),
453
+ 'should call tls.checkServerIdentity with empty host'
454
+ )
455
+ })
456
+
457
+ test('handles null certificate', ({ assert }) => {
458
+ const host = 'example.com'
459
+
460
+ const result = checkServerIdentity(host, null)
461
+ assert.equal(result?.message, 'Certificate is required', 'should handle null certificate')
462
+ })
463
+
464
+ test('verifies function signature and call pattern', ({ assert }) => {
465
+ const host = 'test.example.com'
466
+ const cert = {
467
+ subject: { CN: 'test.example.com' },
468
+ issuer: { CN: 'Test CA' },
469
+ valid_from: '2024-01-01',
470
+ valid_to: '2025-01-01',
471
+ } as tls.PeerCertificate
472
+
473
+ mockCheckServerIdentity.returns(undefined)
474
+
475
+ checkServerIdentity(host, cert)
476
+
477
+ assert.isTrue(mockCheckServerIdentity.calledOnce, 'should call tls.checkServerIdentity exactly once')
478
+ const [calledHost, calledCert] = mockCheckServerIdentity.firstCall.args
479
+ assert.equal(calledHost, host, 'should pass correct host parameter')
480
+ assert.equal(calledCert, cert, 'should pass correct certificate parameter')
481
+ })
482
+ })
@@ -1,8 +1,8 @@
1
1
  import { test } from '@japa/runner'
2
2
  import fs from 'fs'
3
- import { IResponse, Response, CoreEngine, HttpCertificate, DummyLogger } from '../../../../src/index.js'
3
+ import { IResponse, Response, CoreEngine, HttpCertificate, createLogger } from '../../../../src/index.js'
4
4
 
5
- const logger = new DummyLogger()
5
+ const logger = createLogger()
6
6
 
7
7
  // TODO: Move to generating certs on the fly.
8
8
  test.group('Client certificate', (group) => {