Skip to content

Commit

Permalink
test: update faas tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jyecusch authored and tjholm committed Sep 15, 2021
1 parent 66deda9 commit 352239c
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 258 deletions.
167 changes: 22 additions & 145 deletions src/faas/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ import {
TopicTriggerContext as GrpcTopicTriggerContext,
TriggerRequest,
} from '../interfaces/faas';
import { NitricTrigger } from './trigger';
import { HttpTriggerContext, TopicTriggerContext } from './trigger-context';
import { Response } from './response';
import { TriggerContext, HttpContext, EventContext } from './context';

describe('NitricTrigger.fromGrpcTriggerRequest', () => {
describe('From a HttpTriggerRequest', () => {
let trigger: NitricTrigger<string>;
let trigger: TriggerContext;

beforeAll(() => {
const ctx = new GrpcHttpTriggerRequest();
Expand All @@ -34,32 +32,37 @@ describe('NitricTrigger.fromGrpcTriggerRequest', () => {
request.setData('Hello World');
request.setHttp(ctx);

trigger = NitricTrigger.fromGrpcTriggerRequest(request);
trigger = TriggerContext.fromGrpcTriggerRequest(request);
});

it('should have the provided data', () => {
expect(trigger.data).toBe('Hello World');
it('should be HttpContext', () => {
expect(trigger instanceof HttpContext).toBeTruthy();
});

it('should have HTTP context', () => {
expect(trigger.context.isHttp()).toBe(true);
expect(trigger.http).not.toBeUndefined();
});


it('should have the triggers HTTP Method', () => {
expect(trigger.context.asHttp().method).toBe('GET');
expect(trigger.http.req.method).toBe('GET');
});

it('should have the provided data', () => {
expect(trigger.http.req.body).toBe('Hello World');
});

it('should have to provided headers', () => {
expect(trigger.context.asHttp().headers['test']).toBe('test');
expect(trigger.http.req.headers['test']).toBe(['test']);
});

it('should have to provided query params', () => {
expect(trigger.context.asHttp().query['test']).toBe('test');
expect(trigger.http.req.query['test']).toBe('test');
});
});

describe('From a TopicTriggerRequest', () => {
let trigger: NitricTrigger<string>;
let trigger: TriggerContext;

beforeAll(() => {
const ctx = new GrpcTopicTriggerContext();
Expand All @@ -69,145 +72,19 @@ describe('NitricTrigger.fromGrpcTriggerRequest', () => {
request.setData('Hello World');
request.setTopic(ctx);

trigger = NitricTrigger.fromGrpcTriggerRequest(request);
});

it('should have topic trigger context', () => {
expect(trigger.context.isTopic()).toBe(true);
});

it('should have the topic name that raised the trigger', () => {
expect(trigger.context.asTopic().topic).toBe('test');
});
});
});

describe('NitricTrigger.defaultResponse', () => {
describe('Given a trigger with HttpContext', () => {
let response: Response<any>;

beforeAll(() => {
const ctx = new HttpTriggerContext(
'POST',
'/test/',
{ test: 'test' },
{ test: 'test' }
);

const trigger = new NitricTrigger<string>('testing', ctx);
response = trigger.defaultResponse();
});

it('The default response should have HTTP Context', () => {
expect(response.context.isHttp()).toBe(true);
});
});

describe('Given a trigger with TopicContext', () => {
let response: Response<any>;

beforeAll(() => {
const ctx = new TopicTriggerContext('test');

const trigger = new NitricTrigger<string>('testing', ctx);
response = trigger.defaultResponse();
});

it('The default response should have Topic Context', () => {
expect(response.context.isTopic()).toBe(true);
});
});

describe('Given a trigger with no context', () => {
let trigger: NitricTrigger<any>;

beforeAll(() => {
trigger = new NitricTrigger<string>('testing', null);
});

it('Should throw', () => {
expect(trigger.defaultResponse).toThrow();
});
});
});

describe('Nitric Trigger Tests', () => {
describe('Given an empty HTTP request', () => {
const headers = {
'x-nitric-request-id': 'testID',
'x-nitric-payload-type': 'testPayload',
'x-nitric-source': 'testSource',
'x-nitric-source-type': 'REQUEST',
};

const trigger = new NitricTrigger<any>(
'',
new HttpTriggerContext('GET', '/test/', headers, {})
);

test('NitricRequest.getObject, should throw', () => {
expect(trigger.dataAsObject).toThrow('Unexpected end of JSON input');
});

test('NitricRequest.getString, should be empty', () => {
expect(trigger.getString()).toBe('');
});

test('trigger.context.isHttp should be true', () => {
expect(trigger.context.isHttp()).toBe(true);
trigger = TriggerContext.fromGrpcTriggerRequest(request);
});

test('trigger.context.isTopic should be false', () => {
expect(trigger.context.isTopic()).toBe(false);
it('should return an EventTriggerContext', () => {
expect(trigger instanceof EventContext).toBeTruthy();
});
});

describe('Given a JSON HTTP request', () => {
const headers = {
'x-nitric-request-id': 'testID',
'x-nitric-payload-type': 'testPayload',
'x-nitric-source': 'testSource',
'x-nitric-source-type': 'REQUEST',
};

const testObject = {
test: 'test',
};

const encoder = new TextEncoder();

const trigger = new NitricTrigger<typeof testObject>(
encoder.encode(JSON.stringify(testObject)),
new HttpTriggerContext('POST', '/test/', headers, {})
);

test('NitricRequest.getObject should return the provided object', () => {
expect(trigger.dataAsObject()).toEqual(testObject);
it('should have event trigger context', () => {
expect(trigger.event).not.toBeUndefined();
});
});

describe('Given a plain text HTTP request', () => {
const headers = {
'x-nitric-request-id': 'testID',
'x-nitric-payload-type': 'testPayload',
'x-nitric-source': 'testSource',
'x-nitric-source-type': 'REQUEST',
};

const testObject = 'test';

const trigger = new NitricTrigger<typeof testObject>(
testObject,
new HttpTriggerContext('POST', '/test/', headers, {})
);

test('NitricRequest.getObject, should throw', () => {
// Fix typings here...
expect(trigger.dataAsObject).toThrow('Unexpected token');
});

test('NitricRequest.getString, should equal the provided string', () => {
expect(trigger.getString()).toEqual(testObject);
it('should have the topic name that raised the trigger', () => {
expect(trigger.event.req.topic).toBe('test');
});
});
});
42 changes: 27 additions & 15 deletions src/faas/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { faas } from '../interfaces';
import { TriggerResponse } from 'src/interfaces/faas';

export abstract class TriggerContext<Req extends AbstractRequest = AbstractRequest, Resp extends Record<string, any> = any> {
protected request: Req;
Expand Down Expand Up @@ -90,13 +89,25 @@ interface EventResponse {
success: boolean;
}

type Method = 'GET' | 'POST' | 'DELETE' | 'PATCH' | 'PUT' | 'HEAD';

interface HttpRequestArgs {
data: string | Uint8Array,
method: Method | string;
path: string;
query: Record<string, string>;
headers: Record<string, string[]>;
}

export class HttpRequest extends AbstractRequest {
public readonly method: Method | string;
public readonly path: string;
public readonly query: Record<string, string>;
public readonly headers: Record<string, string[]>;

constructor(data: string | Uint8Array , path: string, query: Record<string, string>, headers: Record<string, string[]>) {
constructor({ data, method, path, query, headers }: HttpRequestArgs) {
super(data);
this.method = method;
this.path = path;
this.query = query;
this.headers = headers;
Expand Down Expand Up @@ -129,18 +140,19 @@ export class HttpContext extends TriggerContext<HttpRequest, HttpResponse> {
const http = trigger.getHttp();
const ctx = new HttpContext();

ctx.request = new HttpRequest(
trigger.getData(),
http.getPath(),
http
.getQueryParamsMap()
.toArray()
.reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
http
.getHeadersMap()
.toArray()
.reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {})
);
ctx.request = new HttpRequest({
data: trigger.getData(),
path: http.getPath(),
query: http
.getQueryParamsMap()
.toArray()
.reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
headers: http
.getHeadersMap()
.toArray()
.reduce((acc, [key, val]) => ({ ...acc, [key]: [val] }), {}),
method: http.getMethod(),
});

ctx.response = {
status: 200,
Expand Down Expand Up @@ -190,7 +202,7 @@ export class EventContext extends TriggerContext<EventRequest, EventResponse> {

static toGrpcTriggerResponse(ctx: TriggerContext): faas.TriggerResponse {
const evtCtx = ctx.event;
const triggerResponse = new TriggerResponse();
const triggerResponse = new faas.TriggerResponse();
const topicResponse = new faas.TopicResponseContext();
topicResponse.setSuccess(evtCtx.res.success);
triggerResponse.setTopic(topicResponse);
Expand Down
Loading

0 comments on commit 352239c

Please sign in to comment.