Skip to content

Commit 16cc25b

Browse files
committed
- Updated test cases
- Fixed capture of error body in Angular
1 parent 8915770 commit 16cc25b

19 files changed

+460
-326
lines changed

Diff for: src/templates/core/angular/request.hbs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { HttpClient, HttpHeaders } from '@angular/common/http';
44
import type { HttpResponse, HttpErrorResponse } from '@angular/common/http';
5-
import { catchError, forkJoin, map, mergeMap, of, throwError } from 'rxjs';
5+
import { catchError, forkJoin, map, switchMap, of, throwError } from 'rxjs';
66
import type { Observable } from 'rxjs';
77

88
import { ApiError } from './ApiError';
@@ -72,7 +72,7 @@ export const request = <T>(config: OpenAPIConfig, http: HttpClient, options: Api
7272
const body = getRequestBody(options);
7373

7474
return getHeaders(config, options).pipe(
75-
mergeMap(headers => {
75+
switchMap(headers => {
7676
return sendRequest<T>(config, options, http, url, formData, body, headers);
7777
}),
7878
map(response => {
@@ -88,22 +88,22 @@ export const request = <T>(config: OpenAPIConfig, http: HttpClient, options: Api
8888
}),
8989
catchError((error: HttpErrorResponse) => {
9090
if (!error.status) {
91-
return throwError(() => error);
91+
return throwError(error);
9292
}
9393
return of({
9494
url,
9595
ok: error.ok,
9696
status: error.status,
9797
statusText: error.statusText,
98-
body: error.statusText,
98+
body: error.error ?? error.statusText,
9999
} as ApiResult);
100100
}),
101101
map(result => {
102102
catchErrorCodes(options, result);
103103
return result.body as T;
104104
}),
105105
catchError((error: ApiError) => {
106-
return throwError(() => error);
106+
return throwError(error);
107107
}),
108108
);
109109
};

Diff for: test/e2e/client.axios.spec.ts

+65-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { compileWithTypescript } from './scripts/compileWithTypescript';
33
import { generateClient } from './scripts/generateClient';
44
import server from './scripts/server';
55

6-
describe('v3.node', () => {
6+
describe('client.node', () => {
77
beforeAll(async () => {
88
cleanup('client/axios');
99
await generateClient('client/axios', 'v3', 'axios', false, false, 'AppClient');
@@ -83,4 +83,68 @@ describe('v3.node', () => {
8383
}
8484
expect(error).toContain('Request aborted');
8585
});
86+
87+
it('should throw known error (500)', async () => {
88+
let error;
89+
try {
90+
const { AppClient } = require('./generated/client/axios/index.js');
91+
const client = new AppClient();
92+
await client.error.testErrorCode(500);
93+
} catch (e) {
94+
const err = e as any;
95+
error = JSON.stringify({
96+
name: err.name,
97+
message: err.message,
98+
url: err.url,
99+
status: err.status,
100+
statusText: err.statusText,
101+
body: err.body,
102+
});
103+
}
104+
expect(error).toBe(
105+
JSON.stringify({
106+
name: 'ApiError',
107+
message: 'Custom message: Internal Server Error',
108+
url: 'http://localhost:3000/base/api/v1.0/error?status=500',
109+
status: 500,
110+
statusText: 'Internal Server Error',
111+
body: {
112+
status: 500,
113+
message: 'hello world',
114+
},
115+
})
116+
);
117+
});
118+
119+
it('should throw unknown error (409)', async () => {
120+
let error;
121+
try {
122+
const { AppClient } = require('./generated/client/axios/index.js');
123+
const client = new AppClient();
124+
await client.error.testErrorCode(409);
125+
} catch (e) {
126+
const err = e as any;
127+
error = JSON.stringify({
128+
name: err.name,
129+
message: err.message,
130+
url: err.url,
131+
status: err.status,
132+
statusText: err.statusText,
133+
body: err.body,
134+
});
135+
}
136+
expect(error).toBe(
137+
JSON.stringify({
138+
name: 'ApiError',
139+
message: 'Generic Error',
140+
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
141+
status: 409,
142+
statusText: 'Conflict',
143+
body: {
144+
status: 409,
145+
message: 'hello world',
146+
},
147+
})
148+
);
149+
});
86150
});

Diff for: test/e2e/client.babel.spec.ts

+114-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { copyAsset } from './scripts/copyAsset';
55
import { generateClient } from './scripts/generateClient';
66
import server from './scripts/server';
77

8-
describe('v3.babel', () => {
8+
describe('client.babel', () => {
99
beforeAll(async () => {
1010
cleanup('client/babel');
1111
await generateClient('client/babel', 'v3', 'fetch', true, true, 'AppClient');
@@ -53,13 +53,123 @@ describe('v3.babel', () => {
5353
const { AppClient } = (window as any).api;
5454
const client = new AppClient();
5555
return await client.complex.complexTypes({
56-
first: {
57-
second: {
58-
third: 'Hello World!',
56+
parameterObject: {
57+
first: {
58+
second: {
59+
third: 'Hello World!',
60+
},
5961
},
6062
},
6163
});
6264
});
6365
expect(result).toBeDefined();
6466
});
67+
68+
it('support form data', async () => {
69+
const result = await browser.evaluate(async () => {
70+
const { AppClient } = (window as any).api;
71+
const client = new AppClient();
72+
return await client.parameters.callWithParameters({
73+
parameterHeader: 'valueHeader',
74+
parameterQuery: 'valueQuery',
75+
parameterForm: 'valueForm',
76+
parameterCookie: 'valueCookie',
77+
parameterPath: 'valuePath',
78+
requestBody: {
79+
prop: 'valueBody',
80+
},
81+
});
82+
});
83+
expect(result).toBeDefined();
84+
});
85+
86+
it('can abort the request', async () => {
87+
let error;
88+
try {
89+
await browser.evaluate(async () => {
90+
const { AppClient } = (window as any).api;
91+
const client = new AppClient();
92+
const promise = client.simple.getCallWithoutParametersAndResponse();
93+
setTimeout(() => {
94+
promise.cancel();
95+
}, 10);
96+
await promise;
97+
});
98+
} catch (e) {
99+
error = (e as Error).message;
100+
}
101+
expect(error).toContain('CancelError: Request aborted');
102+
});
103+
104+
it('should throw known error (500)', async () => {
105+
const error = await browser.evaluate(async () => {
106+
try {
107+
const { AppClient } = (window as any).api;
108+
const client = new AppClient();
109+
await client.error.testErrorCode({
110+
status: 500,
111+
});
112+
} catch (e) {
113+
const error = e as any;
114+
return JSON.stringify({
115+
name: error.name,
116+
message: error.message,
117+
url: error.url,
118+
status: error.status,
119+
statusText: error.statusText,
120+
body: error.body,
121+
});
122+
}
123+
return;
124+
});
125+
expect(error).toBe(
126+
JSON.stringify({
127+
name: 'ApiError',
128+
message: 'Custom message: Internal Server Error',
129+
url: 'http://localhost:3000/base/api/v1.0/error?status=500',
130+
status: 500,
131+
statusText: 'Internal Server Error',
132+
body: {
133+
status: 500,
134+
message: 'hello world',
135+
},
136+
})
137+
);
138+
});
139+
140+
it('should throw unknown error (409)', async () => {
141+
const error = await browser.evaluate(async () => {
142+
try {
143+
const { AppClient } = (window as any).api;
144+
const client = new AppClient();
145+
await client.error.testErrorCode({
146+
status: 409,
147+
});
148+
} catch (e) {
149+
const error = e as any;
150+
return JSON.stringify({
151+
name: error.name,
152+
message: error.message,
153+
url: error.url,
154+
status: error.status,
155+
statusText: error.statusText,
156+
body: error.body,
157+
});
158+
}
159+
return;
160+
});
161+
expect(error).toBe(
162+
JSON.stringify({
163+
name: 'ApiError',
164+
message: 'Generic Error',
165+
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
166+
status: 409,
167+
statusText: 'Conflict',
168+
body: {
169+
status: 409,
170+
message: 'hello world',
171+
},
172+
})
173+
);
174+
});
65175
});

Diff for: test/e2e/client.fetch.spec.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { copyAsset } from './scripts/copyAsset';
55
import { generateClient } from './scripts/generateClient';
66
import server from './scripts/server';
77

8-
describe('v3.fetch', () => {
8+
describe('client.fetch', () => {
99
beforeAll(async () => {
1010
cleanup('client/fetch');
1111
await generateClient('client/fetch', 'v3', 'fetch', false, false, 'AppClient');
@@ -126,7 +126,10 @@ describe('v3.fetch', () => {
126126
url: 'http://localhost:3000/base/api/v1.0/error?status=500',
127127
status: 500,
128128
statusText: 'Internal Server Error',
129-
body: 'Internal Server Error',
129+
body: {
130+
status: 500,
131+
message: 'hello world',
132+
},
130133
})
131134
);
132135
});
@@ -157,7 +160,10 @@ describe('v3.fetch', () => {
157160
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
158161
status: 409,
159162
statusText: 'Conflict',
160-
body: 'Conflict',
163+
body: {
164+
status: 409,
165+
message: 'hello world',
166+
},
161167
})
162168
);
163169
});

Diff for: test/e2e/client.node.spec.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { compileWithTypescript } from './scripts/compileWithTypescript';
33
import { generateClient } from './scripts/generateClient';
44
import server from './scripts/server';
55

6-
describe('v3.node', () => {
6+
describe('client.node', () => {
77
beforeAll(async () => {
88
cleanup('client/node');
99
await generateClient('client/node', 'v3', 'node', false, false, 'AppClient');
@@ -108,7 +108,10 @@ describe('v3.node', () => {
108108
url: 'http://localhost:3000/base/api/v1.0/error?status=500',
109109
status: 500,
110110
statusText: 'Internal Server Error',
111-
body: 'Internal Server Error',
111+
body: {
112+
status: 500,
113+
message: 'hello world',
114+
},
112115
})
113116
);
114117
});
@@ -137,7 +140,10 @@ describe('v3.node', () => {
137140
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
138141
status: 409,
139142
statusText: 'Conflict',
140-
body: 'Conflict',
143+
body: {
144+
status: 409,
145+
message: 'hello world',
146+
},
141147
})
142148
);
143149
});

Diff for: test/e2e/client.xhr.spec.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { copyAsset } from './scripts/copyAsset';
55
import { generateClient } from './scripts/generateClient';
66
import server from './scripts/server';
77

8-
describe('v3.xhr', () => {
8+
describe('client.xhr', () => {
99
beforeAll(async () => {
1010
cleanup('client/xhr');
1111
await generateClient('client/xhr', 'v3', 'xhr', false, false, 'AppClient');
@@ -63,6 +63,24 @@ describe('v3.xhr', () => {
6363
expect(result).toBeDefined();
6464
});
6565

66+
it('support form data', async () => {
67+
const result = await browser.evaluate(async () => {
68+
const { AppClient } = (window as any).api;
69+
const client = new AppClient();
70+
return await client.parameters.callWithParameters(
71+
'valueHeader',
72+
'valueQuery',
73+
'valueForm',
74+
'valueCookie',
75+
'valuePath',
76+
{
77+
prop: 'valueBody',
78+
}
79+
);
80+
});
81+
expect(result).toBeDefined();
82+
});
83+
6684
it('can abort the request', async () => {
6785
let error;
6886
try {
@@ -107,7 +125,10 @@ describe('v3.xhr', () => {
107125
url: 'http://localhost:3000/base/api/v1.0/error?status=500',
108126
status: 500,
109127
statusText: 'Internal Server Error',
110-
body: 'Internal Server Error',
128+
body: {
129+
status: 500,
130+
message: 'hello world',
131+
},
111132
})
112133
);
113134
});
@@ -138,7 +159,10 @@ describe('v3.xhr', () => {
138159
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
139160
status: 409,
140161
statusText: 'Conflict',
141-
body: 'Conflict',
162+
body: {
163+
status: 409,
164+
message: 'hello world',
165+
},
142166
})
143167
);
144168
});

Diff for: test/e2e/scripts/server.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ const start = async (dir: string) => {
5656
// See the spec files for more information.
5757
_app.all('/base/api/v1.0/error', (req, res) => {
5858
const status = parseInt(String(req.query.status));
59-
res.sendStatus(status);
59+
res.status(status).json({
60+
status,
61+
message: 'hello world',
62+
});
6063
});
6164

6265
// Register an 'echo' server that just returns all data from the API calls.

0 commit comments

Comments
 (0)