| 
									
										
										
										
											2025-07-28 23:20:52 +00:00
										 |  |  | import { tap, expect } from '@git.zone/tstest/tapbundle'; | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // For browser tests, we need to import from a browser-safe path
 | 
					
						
							|  |  |  | // that doesn't trigger Node.js module imports
 | 
					
						
							|  |  |  | import { CoreRequest, CoreResponse } from '../ts/core/index.js'; | 
					
						
							|  |  |  | import type { ICoreRequestOptions } from '../ts/core_base/types.js'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tap.test('browser: should request a JSON document over https', async () => { | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  |   const request = new CoreRequest( | 
					
						
							|  |  |  |     'https://jsonplaceholder.typicode.com/posts/1', | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   const response = await request.fire(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   expect(response).not.toBeNull(); | 
					
						
							|  |  |  |   expect(response).toHaveProperty('status'); | 
					
						
							|  |  |  |   expect(response.status).toEqual(200); | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   const data = await response.json(); | 
					
						
							|  |  |  |   expect(data).toHaveProperty('id'); | 
					
						
							|  |  |  |   expect(data.id).toEqual(1); | 
					
						
							|  |  |  |   expect(data).toHaveProperty('title'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tap.test('browser: should handle CORS requests', async () => { | 
					
						
							|  |  |  |   const options: ICoreRequestOptions = { | 
					
						
							|  |  |  |     headers: { | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  |       Accept: 'application/vnd.github.v3+json', | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const request = new CoreRequest( | 
					
						
							|  |  |  |     'https://api.github.com/users/github', | 
					
						
							|  |  |  |     options, | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   const response = await request.fire(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   expect(response).not.toBeNull(); | 
					
						
							|  |  |  |   expect(response.status).toEqual(200); | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   const data = await response.json(); | 
					
						
							|  |  |  |   expect(data).toHaveProperty('login'); | 
					
						
							|  |  |  |   expect(data.login).toEqual('github'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tap.test('browser: should handle request timeouts', async () => { | 
					
						
							|  |  |  |   let timedOut = false; | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   const options: ICoreRequestOptions = { | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  |     timeout: 1, // Extremely short timeout to guarantee failure
 | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   try { | 
					
						
							| 
									
										
										
										
											2025-07-29 15:44:04 +00:00
										 |  |  |     // Use a URL that will definitely take longer than 1ms
 | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  |     const request = new CoreRequest( | 
					
						
							|  |  |  |       'https://jsonplaceholder.typicode.com/posts/1', | 
					
						
							|  |  |  |       options, | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |     await request.fire(); | 
					
						
							|  |  |  |   } catch (error) { | 
					
						
							|  |  |  |     timedOut = true; | 
					
						
							| 
									
										
										
										
											2025-07-29 15:44:04 +00:00
										 |  |  |     // Accept any error since different browsers handle timeouts differently
 | 
					
						
							|  |  |  |     expect(error).toBeDefined(); | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   expect(timedOut).toEqual(true); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tap.test('browser: should handle POST requests with JSON', async () => { | 
					
						
							|  |  |  |   const testData = { | 
					
						
							|  |  |  |     title: 'foo', | 
					
						
							|  |  |  |     body: 'bar', | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  |     userId: 1, | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   const options: ICoreRequestOptions = { | 
					
						
							|  |  |  |     method: 'POST', | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  |     requestBody: testData, | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const request = new CoreRequest( | 
					
						
							|  |  |  |     'https://jsonplaceholder.typicode.com/posts', | 
					
						
							|  |  |  |     options, | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   const response = await request.fire(); | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   expect(response.status).toEqual(201); | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   const responseData = await response.json(); | 
					
						
							|  |  |  |   expect(responseData).toHaveProperty('id'); | 
					
						
							|  |  |  |   expect(responseData.title).toEqual(testData.title); | 
					
						
							|  |  |  |   expect(responseData.body).toEqual(testData.body); | 
					
						
							|  |  |  |   expect(responseData.userId).toEqual(testData.userId); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tap.test('browser: should handle query parameters', async () => { | 
					
						
							|  |  |  |   const options: ICoreRequestOptions = { | 
					
						
							|  |  |  |     queryParams: { | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  |       userId: '2', | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const request = new CoreRequest( | 
					
						
							|  |  |  |     'https://jsonplaceholder.typicode.com/posts', | 
					
						
							|  |  |  |     options, | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   const response = await request.fire(); | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   expect(response.status).toEqual(200); | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   const data = await response.json(); | 
					
						
							| 
									
										
										
										
											2025-07-29 13:49:50 +00:00
										 |  |  |   expect(Array.isArray(data)).toBeTrue(); | 
					
						
							|  |  |  |   // Verify we got posts filtered by userId 2
 | 
					
						
							|  |  |  |   if (data.length > 0) { | 
					
						
							|  |  |  |     expect(data[0]).toHaveProperty('userId'); | 
					
						
							|  |  |  |     expect(data[0].userId).toEqual(2); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | export default tap.start(); |