Skip to content

Commit

Permalink
Fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
YanagiEiichi committed Jul 8, 2024
1 parent 3f9e41d commit c2b853b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 60 deletions.
2 changes: 1 addition & 1 deletion packages/builder/src/tests/codegen/all-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const typeVector = describe.each([
['java.util.List', 'unknown[]', 'NSArray<NSObject*>*'],
['java.util.List<java.lang.Long>', 'Long[]', 'NSArray<NSNumber*>*'],
['java.util.List<java.lang.Void>', 'void[]', 'NSArray<void*>*'],
['java.util.Map<java.lang.Long, java.lang.Long>', 'Record<Long, Long>', 'NSDictionary*'],
['java.util.Map<java.lang.Long, java.lang.Long>', 'Record<Long, Long | undefined>', 'NSDictionary*'],
['groovy.lang.Tuple2<java.lang.String, java.lang.Long>', '[ string, Long ]', 'NSArray<NSObject*>*'],
['java.util.List<java.lang.ThreadLocal<java.lang.Long>>', 'Optional<Long>[]', 'NSArray<NSNumber*>*' ],
['java.util.List<java.util.Optional<java.lang.Long>>', 'Optional<Long>[]', 'NSArray<NSNumber*>*' ],
Expand Down
60 changes: 14 additions & 46 deletions packages/builder/src/tests/codegen/ts-special-map-key.test.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,17 @@
import { buildTsMethodWithParameters } from '../test-tools/buildMethodWithParameters';

test('java.util.Map<test.UserType, java.lang.Long>', () => {
const { currentTestName: type = '' } = expect.getState();
const code = buildTsMethodWithParameters({ name: 'map', type });
expect(code).toContain(`map?: Record<UserType, Long>`);
});

test('java.util.Map<test.User, java.lang.Long>', () => {
const { currentTestName: type = '' } = expect.getState();
const code = buildTsMethodWithParameters({ name: 'map', type });
expect(code).toContain(`map?: Record<PropertyKey, Long>`);
});

test('java.util.Map<java.lang.Long, java.lang.Long>', () => {
const { currentTestName: type = '' } = expect.getState();
const code = buildTsMethodWithParameters({ name: 'map', type });
expect(code).toContain(`map?: Record<Long, Long>`);
});

test('java.util.Map<java.lang.Integer, java.lang.Long>', () => {
const { currentTestName: type = '' } = expect.getState();
const code = buildTsMethodWithParameters({ name: 'map', type });
expect(code).toContain(`map?: Record<number, Long>`);
});

test('java.util.Map<java.lang.Double, java.lang.Long>', () => {
const { currentTestName: type = '' } = expect.getState();
const code = buildTsMethodWithParameters({ name: 'map', type });
expect(code).toContain(`map?: Record<number, Long>`);
});

test('java.util.Map<java.lang.String, java.lang.Long>', () => {
const { currentTestName: type = '' } = expect.getState();
const code = buildTsMethodWithParameters({ name: 'map', type });
expect(code).toContain(`map?: Record<string, Long>`);
});

test('java.util.Map<java.math.BigDecimal, java.lang.Long>', () => {
const { currentTestName: type = '' } = expect.getState();
const code = buildTsMethodWithParameters({ name: 'map', type });
expect(code).toContain(`map?: Record<BigDecimal, Long>`);
});

test('java.util.Map<java.math.BigInteger, java.lang.Long>', () => {
const { currentTestName: type = '' } = expect.getState();
const code = buildTsMethodWithParameters({ name: 'map', type });
expect(code).toContain(`map?: Record<BigInteger, Long>`);
test.each([
['test.UserType', 'UserType'],
['test.User', 'PropertyKey'],
['java.lang.Long', 'Long'],
['java.lang.Integer', 'number'],
['java.lang.Double', 'number'],
['java.lang.String', 'string'],
['java.math.BigDecimal', 'BigDecimal'],
['java.math.BigInteger', 'BigInteger'],
])('java.util.Map<%s, java.lang.Long>', (keyType, tsType) => {
const code = buildTsMethodWithParameters({ name: 'map', type: `java.util.Map<${keyType}, java.lang.Long>` });
expect(code).toMatchCode(
`async foo(map?: Record<${tsType}, Long | undefined>, settings?: Partial<Settings>) {`,
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const buildMethodWithParameters = (target: 'oc' | 'ts', ...parameters: De
};
const defs = { routes: [foo], classes: [User], enums: [UserType] };
const { code } = new Builder({ target, base: '', defs });
return code.replace(/\s+/g, ' ');
return code;
};

export const buildTsMethodWithParameters = (...parameters: DeepPartial<NadParameter>[]) =>
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime/src/NadInvoker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
APPLICATION_JSON,
CONTENT_TYPE,
InvokeParams,
InvokeResult,
WWW_FORM_URLENCODED,
Expand Down Expand Up @@ -294,7 +294,7 @@ export class NadInvoker<T> implements NadRuntime<T> {
//
const headers = { ...runtime.headers, ...this.headers, ...settings?.headers };

const contentType = insensitiveGet(headers, 'Content-Type');
const contentType = insensitiveGet(headers, CONTENT_TYPE);

const uri = joinPath(base, buildPath(rawPath, pathVariables));

Expand Down Expand Up @@ -332,7 +332,7 @@ export class NadInvoker<T> implements NadRuntime<T> {
// In this case, the data can be passed using payload.
const tIsWwwFormUrlEncoded = contentType && isWwwFormUrlEncoded(contentType);
if (canTakePayload && (!contentType || tIsWwwFormUrlEncoded || tIsMultipartFormData)) {
if (!contentType) headers['Content-Type'] = WWW_FORM_URLENCODED;
if (!contentType) headers[CONTENT_TYPE] = WWW_FORM_URLENCODED;
const data = requestParams;
const qs = prependQuestionMarkIfNotEmpty(buildQs(staticParams));
return { method, url: uri + qs, timeout, headers, data, ...extensions };
Expand Down
12 changes: 6 additions & 6 deletions packages/runtime/src/tests/NadInvoker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe('addRequestParam', () => {
method: 'POST',
url: `${base}/test`,
data: 'id=123&name=hehe',
headers: { 'Content-Type': WWW_FORM_URLENCODED },
headers: { [CONTENT_TYPE]: WWW_FORM_URLENCODED },
});
});

Expand Down Expand Up @@ -182,7 +182,7 @@ describe('addStaticParam', () => {
method: 'POST',
url: `${base}/test?action=wtf`,
data: 'name=hehe',
headers: { 'Content-Type': WWW_FORM_URLENCODED },
headers: { [CONTENT_TYPE]: WWW_FORM_URLENCODED },
});
});
});
Expand All @@ -199,7 +199,7 @@ describe('addRequestBody', () => {

test('qs data object in body', async () => {
const res = await new Localhost()
.open('POST', '/test', { headers: { 'Content-Type': WWW_FORM_URLENCODED } })
.open('POST', '/test', { headers: { [CONTENT_TYPE]: WWW_FORM_URLENCODED } })
.addRequestBody({ a: 1 })
.execute();
expect(res).toMatchObject({
Expand All @@ -211,7 +211,7 @@ describe('addRequestBody', () => {

test('form data object in body', async () => {
const res = await new Localhost()
.open('POST', '/test', { headers: { 'Content-Type': MULTIPART_FORM_DATA } })
.open('POST', '/test', { headers: { [CONTENT_TYPE]: MULTIPART_FORM_DATA } })
.addRequestBody({ a: 1 })
.execute();
expect(res).toMatchObject({
Expand Down Expand Up @@ -250,7 +250,7 @@ describe('addModelAttribute', () => {
test('with json', async () => {
const res = await new Localhost()
.open('POST', '/test')
.addHeader('Content-Type', APPLICATION_JSON)
.addHeader(CONTENT_TYPE, APPLICATION_JSON)
.addModelAttribute({ a: 1, b: { c: 2 } })
.execute();
expect(res).toMatchObject({
Expand Down Expand Up @@ -307,7 +307,7 @@ describe('addModelAttribute', () => {
method: 'POST',
url: `${base}/test`,
data: 'user.name=hehe&user.age=18&user.v=1&user.v=2',
headers: { 'Content-Type': WWW_FORM_URLENCODED },
headers: { [CONTENT_TYPE]: WWW_FORM_URLENCODED },
});
});

Expand Down
6 changes: 3 additions & 3 deletions packages/runtime/src/tests/libs/mock-xhr.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventEmitter } from 'events';
import { readAsDataURL } from './readAsDataURL';
import { APPLICATION_JSON, MULTIPART_FORM_DATA } from '@huolala-tech/request';
import { APPLICATION_JSON, CONTENT_TYPE, MULTIPART_FORM_DATA } from '@huolala-tech/request';

export const MULTIPART_FORM_DATA_WITH_BOUNDARY = `${MULTIPART_FORM_DATA}; boundary=----WebKitFormBoundaryHehehehe`;

Expand Down Expand Up @@ -63,7 +63,7 @@ global.XMLHttpRequest = class {
}
} else if (body instanceof FormData) {
if (!Object.keys(this.headers).some((s) => /^Content-Type$/i.test(s))) {
this.headers['Content-Type'] = MULTIPART_FORM_DATA_WITH_BOUNDARY;
this.headers[CONTENT_TYPE] = MULTIPART_FORM_DATA_WITH_BOUNDARY;
}
const temp: Record<string, unknown> = {};
const tasks = Array.from(body, async ([k, v]) => {
Expand Down Expand Up @@ -96,7 +96,7 @@ global.XMLHttpRequest = class {
this.headers[key] = value;
}
getResponseHeader(key: string) {
if (key === 'Content-Type') return APPLICATION_JSON;
if (key === CONTENT_TYPE) return APPLICATION_JSON;
return null;
}
getAllResponseHeaders() {
Expand Down

0 comments on commit c2b853b

Please sign in to comment.