Skip to content

Commit

Permalink
feat: add import of global asserts and import of onResponse Hooks (An…
Browse files Browse the repository at this point in the history
AnWeber committed Jul 16, 2023
1 parent a50dd49 commit c7acd66
Showing 9 changed files with 70 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@
### Features

- add method `$getOAuth2Response` to javascript context (#499)
- add `HttpClientProvider` and `JavascriptProvider` to httpyac API
- allow global Asserts and `onRequest`/ `onResponse` hooks and import global Asserts from other Http Files (#488)

## [6.5.1] (2023-06-13)

1 change: 1 addition & 0 deletions src/models/httpFile.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ export interface HttpFile {
readonly rootDir?: PathLike;
readonly hooks: HttpFileHooks;
readonly httpRegions: Array<HttpRegion>;
readonly globalHttpRegions: Array<HttpRegion>;
activeEnvironment?: string[];

findHttpRegion(name: string): HttpRegion | undefined;
13 changes: 8 additions & 5 deletions src/models/httpRegion.ts
Original file line number Diff line number Diff line change
@@ -22,6 +22,13 @@ export type PartialProperty<T, TProperty extends string, TProperty2 extends stri
> &
Partial<T>;

export interface RequestClientHooks {
onRequest: OnRequestHook;
onStreaming: OnStreaming;
onResponse: OnResponseHook;
responseLogging: ResponseLoggingHook;
}

export interface HttpRegion {
request?: Request;
response?: HttpResponse;
@@ -30,12 +37,8 @@ export interface HttpRegion {
testResults?: Array<TestResult>;
responseRefs?: Array<string>;
variablesPerEnv: Record<string, Variables>;
readonly hooks: {
readonly hooks: RequestClientHooks & {
execute: ExecuteHook;
onRequest: OnRequestHook;
onStreaming: OnStreaming;
onResponse: OnResponseHook;
responseLogging: ResponseLoggingHook;
};
isGlobal(): boolean;
clone(httpFile?: HttpFile): HttpRegion;
7 changes: 5 additions & 2 deletions src/plugins/core/execute/regionScopedVariablesInterceptor.ts
Original file line number Diff line number Diff line change
@@ -14,10 +14,13 @@ export class RegionScopedVariablesInterceptor implements HookInterceptor<[models
const context = hookContext.args[0];
const env = toEnvironmentKey(context.httpFile.activeEnvironment);
if (context.config?.useRegionScopedVariables) {
const httpRegions = context.httpFile.httpRegions.filter(obj => obj.isGlobal());
const regionScopedVariables: RegionScopedVariableOptions = context.options;
regionScopedVariables.variables = context.variables;
context.variables = Object.assign({}, context.variables, ...httpRegions.map(obj => obj.variablesPerEnv[env]));
context.variables = Object.assign(
{},
context.variables,
...context.httpFile.globalHttpRegions.map(obj => obj.variablesPerEnv[env])
);
} else {
context.variables = Object.assign(
context.variables,
4 changes: 4 additions & 0 deletions src/store/httpFile.ts
Original file line number Diff line number Diff line change
@@ -22,4 +22,8 @@ export class HttpFile implements models.HttpFile {
public findHttpRegion(name: string): models.HttpRegion | undefined {
return this.httpRegions.find(obj => obj.metaData?.name === name && !obj.metaData.disabled);
}

public get globalHttpRegions() {
return this.httpRegions.filter(obj => obj.isGlobal() && !obj.metaData.disabled);
}
}
11 changes: 2 additions & 9 deletions src/store/httpRegion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as models from '../models';
import { toEnvironmentKey } from '../utils';
import { toEnvironmentKey, addHttpFileRequestClientHooks } from '../utils';
import { Hook, HookCancel } from 'hookpoint';

export class HttpRegion implements models.HttpRegion {
@@ -74,14 +74,7 @@ export class HttpRegion implements models.HttpRegion {
...context,
httpFile: this.httpFile,
httpRegion: this,
hooks: {
onRequest: this.hooks.onRequest.merge(this.httpFile.hooks.onRequest) as models.OnRequestHook,
onResponse: this.hooks.onResponse.merge(this.httpFile.hooks.onResponse) as models.OnResponseHook,
onStreaming: this.hooks.onStreaming.merge(this.httpFile.hooks.onStreaming) as models.OnStreaming,
responseLogging: this.hooks.responseLogging.merge(
this.httpFile.hooks.responseLogging
) as models.ResponseLoggingHook,
},
hooks: addHttpFileRequestClientHooks(this.hooks, this.httpFile),
isMainContext,
});
if (!this.isGlobal()) {
19 changes: 19 additions & 0 deletions src/test/assert/status.spec.ts
Original file line number Diff line number Diff line change
@@ -24,6 +24,25 @@ describe('assert.status', () => {
expect(httpFile.httpRegions[0].testResults?.[0].result).toBeTruthy();
expect(httpFile.httpRegions[0].testResults?.[0].message).toBe('status == 200');
});
it('should be equal to 200 with global assert', async () => {
initFileProvider();
await localServer.forGet('/get').thenReply(200);
const httpFile = await parseHttp(`
?? status == 200
###
GET /get
`);

const responses = await sendHttpFile(httpFile, {
host: `http://localhost:${localServer.port}`,
});
expect(responses.length).toBe(1);
expect(responses[0].statusCode).toBe(200);
expect(httpFile.httpRegions[1].testResults?.length).toBe(1);
expect(httpFile.httpRegions[1].testResults?.[0].result).toBeTruthy();
expect(httpFile.httpRegions[1].testResults?.[0].message).toBe('status == 200');
});
it('should not be equal 200', async () => {
initFileProvider();
await localServer.forGet('/get').thenReply(201);
32 changes: 27 additions & 5 deletions src/utils/httpRegionUtils.ts
Original file line number Diff line number Diff line change
@@ -56,12 +56,34 @@ export async function executeGlobalScripts(context: {
httpFile: models.HttpFile;
options: Record<string, unknown>;
}): Promise<boolean> {
for (const httpRegion of context.httpFile.httpRegions) {
if (httpRegion.isGlobal() && !httpRegion.metaData.disabled) {
if (!(await httpRegion.execute(context))) {
return false;
}
for (const httpRegion of context.httpFile.globalHttpRegions) {
if (!(await httpRegion.execute(context))) {
return false;
}
}
return true;
}

export function addHttpFileRequestClientHooks(
requestClientHooks: models.RequestClientHooks,
httpFile: models.HttpFile
): models.RequestClientHooks {
return {
onRequest: httpFile.hooks.onRequest.merge(
...httpFile.globalHttpRegions.map(obj => obj.hooks.onRequest),
requestClientHooks.onRequest
) as models.OnRequestHook,
onResponse: httpFile.hooks.onResponse.merge(
requestClientHooks.onResponse,
...httpFile.globalHttpRegions.map(obj => obj.hooks.onResponse)
) as models.OnResponseHook,
onStreaming: httpFile.hooks.onStreaming.merge(
requestClientHooks.onStreaming,
...httpFile.globalHttpRegions.map(obj => obj.hooks.onStreaming)
) as models.OnStreaming,
responseLogging: httpFile.hooks.responseLogging.merge(
requestClientHooks.responseLogging,
...httpFile.globalHttpRegions.map(obj => obj.hooks.responseLogging)
) as models.ResponseLoggingHook,
};
}
3 changes: 2 additions & 1 deletion src/utils/processorContextUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { log, fileProvider } from '../io';
import * as models from '../models';
import { executeGlobalScripts } from './httpRegionUtils';
import { addHttpFileRequestClientHooks, executeGlobalScripts } from './httpRegionUtils';
import { replaceFilePath } from './variableUtils';

interface ImportProcessorContext extends models.ProcessorContext {
@@ -60,6 +60,7 @@ export async function importHttpFileInContext(
activeEnvironment: context.httpFile.activeEnvironment,
}
);
context.hooks = addHttpFileRequestClientHooks(context.hooks, ref);
context.options.httpFiles.push({ base: context.httpFile, ref });
return ref;
});

0 comments on commit c7acd66

Please sign in to comment.