Skip to content

Commit 655aa47

Browse files
committed
gh-87 handle scheme in host
1 parent 78cc66b commit 655aa47

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

src/connection/gqlClient.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ export interface GraphQLClient {
77
}
88

99
export const gqlClient = (config: ConnectionParams): GraphQLClient => {
10-
const scheme = config.scheme;
11-
const host = config.host;
1210
const defaultHeaders = config.headers;
11+
const version = '/v1/graphql';
12+
const baseUri = config.host.startsWith(`${config.scheme}://`)
13+
? `${config.host}${version}`
14+
: `${config.scheme}://${config.host}${version}`;
15+
1316
return {
1417
// for backward compatibility with replaced graphql-client lib,
1518
// results are wrapped into { data: data }
1619
query: (query: TQuery, variables?: Variables, headers?: HeadersInit) => {
17-
return new Client(`${scheme}://${host}/v1/graphql`, {
20+
return new Client(baseUri, {
1821
headers: {
1922
...defaultHeaders,
2023
...headers,

src/connection/httpClient.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export interface HttpClient {
1414
}
1515

1616
export const httpClient = (config: ConnectionParams): HttpClient => {
17-
const baseUri = `${config.scheme}://${config.host}/v1`;
17+
const version = '/v1';
18+
const baseUri = config.host.startsWith(`${config.scheme}://`)
19+
? `${config.host}${version}`
20+
: `${config.scheme}://${config.host}${version}`;
1821
const url = makeUrl(baseUri);
1922

2023
return {

src/connection/unit.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,40 @@ describe('mock server auth tests', () => {
124124
await conn.login().then((key) => expect(key).toEqual(apiKey));
125125
});
126126

127+
it('should construct the correct baseUri for conn.http when host contains scheme', async () => {
128+
const apiKey = 'abcd123';
129+
130+
const conn = new Connection({
131+
scheme: 'http',
132+
host: 'http://localhost:' + server.port,
133+
apiKey: new ApiKey(apiKey),
134+
});
135+
136+
await conn.http.get('/testEndpoint');
137+
const lastRequestURL = server.lastRequest().path; // Get the path of the last request
138+
const expectedPath = '/v1/testEndpoint';
139+
expect(lastRequestURL).toEqual(expectedPath);
140+
});
141+
142+
it('should construct the correct baseUri for conn.query when host contains scheme', async () => {
143+
const apiKey = 'abcd123';
144+
145+
const conn = new Connection({
146+
scheme: 'http',
147+
host: 'http://localhost:' + server.port,
148+
apiKey: new ApiKey(apiKey),
149+
});
150+
151+
try {
152+
await conn.query('{ query { users } }');
153+
} catch (error) {
154+
// We just want to test the last request path
155+
}
156+
const lastRequestURL = server.lastRequest().path;
157+
const expectedPath = '/v1/graphql';
158+
expect(lastRequestURL).toEqual(expectedPath);
159+
});
160+
127161
it('shuts down the server', () => {
128162
return server.close();
129163
});

test/server.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function testServer() {
2727
lastRequest = ctx.request;
2828
return next();
2929
});
30-
app.use(getLocalOidcConfig, getRemoteOidcConfig, issueToken);
30+
app.use(getLocalOidcConfig, getRemoteOidcConfig, issueToken, mockGetEndpoint, mockGraphQLResponse);
3131
const server = app.listen(port);
3232

3333
serverCache = {
@@ -46,6 +46,28 @@ export function testServer() {
4646
return serverCache;
4747
}
4848

49+
const mockGetEndpoint = (ctx: any, next: any) => {
50+
if (ctx.path !== '/v1/testEndpoint') {
51+
return next();
52+
}
53+
54+
ctx.response.status = 200;
55+
ctx.response.body = { message: 'test endpoint' };
56+
};
57+
58+
const mockGraphQLResponse = (ctx: any, next: any) => {
59+
if (ctx.path !== '/v1/graphql') {
60+
return next();
61+
}
62+
63+
ctx.response.status = 200;
64+
ctx.response.body = {
65+
data: {
66+
someField: 'someValue',
67+
},
68+
};
69+
};
70+
4971
const getLocalOidcConfig = (ctx: any, next: any) => {
5072
if (ctx.path !== '/v1/.well-known/openid-configuration') {
5173
return next();

0 commit comments

Comments
 (0)