Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export function post(name: string, optionsOrHandler: HttpMethodFunctionOptions |
http(name, convertToHttpOptions(optionsOrHandler, 'POST'));
}

export function query(name: string, optionsOrHandler: HttpMethodFunctionOptions | HttpHandler): void {
http(name, convertToHttpOptions(optionsOrHandler, 'QUERY'));
}

export function patch(name: string, optionsOrHandler: HttpMethodFunctionOptions | HttpHandler): void {
http(name, convertToHttpOptions(optionsOrHandler, 'PATCH'));
}
Expand Down
63 changes: 63 additions & 0 deletions test/app.http.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License.

import 'mocha';
import { FunctionMetadata } from '@azure/functions-core';
import { expect } from 'chai';
import * as sinon from 'sinon';
import * as app from '../src/app';
import * as tryGetCoreApiLazyModule from '../src/utils/tryGetCoreApiLazy';
import { HttpHandler, HttpMethodFunctionOptions } from '../types';

describe('app.query', () => {
const handler: HttpHandler = () => ({ body: 'results' });
const registerFunction = sinon.stub();

beforeEach(() => {
registerFunction.reset();
sinon.stub(tryGetCoreApiLazyModule, 'tryGetCoreApiLazy').returns({
registerFunction,
setProgrammingModel: sinon.stub(),
} as unknown as ReturnType<typeof tryGetCoreApiLazyModule.tryGetCoreApiLazy>);
});

afterEach(() => {
sinon.restore();
});

for (const useOptions of [false, true]) {
it(`registers QUERY with ${useOptions ? 'options' : 'a handler'}`, () => {
const options: HttpMethodFunctionOptions = {
handler,
route: 'search',
authLevel: 'function',
};
app.query('queryFn', useOptions ? options : handler);

sinon.assert.calledOnce(registerFunction);
const [metadata, callback] = registerFunction.firstCall.args as [FunctionMetadata, HttpHandler];
expect(metadata.name).to.equal('queryFn');
const binding = Object.values(metadata.bindings).find((b) => b.type === 'httpTrigger');
expect(binding).to.deep.include({
methods: ['QUERY'],
authLevel: useOptions ? 'function' : 'anonymous',
});
if (useOptions) {
expect(binding).to.include({ route: 'search' });
} else {
expect(binding).not.to.have.property('route');
}
expect(metadata.bindings.$return?.type).to.equal('http');
expect(callback).to.equal(handler);
});
}

it('preserves QUERY alongside other methods in app.http', () => {
app.http('mixedFn', { methods: ['GET', 'QUERY'], handler });

sinon.assert.calledOnce(registerFunction);
const metadata = registerFunction.firstCall.args[0] as FunctionMetadata;
const binding = Object.values(metadata.bindings).find((b) => b.type === 'httpTrigger');
expect(binding).to.deep.include({ methods: ['GET', 'QUERY'] });
});
});
16 changes: 16 additions & 0 deletions test/http/HttpRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ import * as workerLogModule from '../../src/utils/workerSystemLog';
chai.use(chaiAsPromised);

describe('HttpRequest', () => {
it('preserves a QUERY request method and JSON body when cloned', async () => {
const body = { filter: 'active' };
const req = new HttpRequest({
method: 'QUERY',
url: 'http://localhost:7071/api/search',
headers: { 'content-type': 'application/json' },
body: { string: JSON.stringify(body) },
});
const cloned = req.clone();

expect(req.method).to.equal('QUERY');
expect(cloned.method).to.equal('QUERY');
expect(await req.json()).to.deep.equal(body);
expect(await cloned.json()).to.deep.equal(body);
});

it('clone', async () => {
const req = new HttpRequest({
method: 'POST',
Expand Down
18 changes: 18 additions & 0 deletions test/types/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ const invalidServiceBusTopicTriggerOptions: ServiceBusTopicTriggerOptions = {

declare const app: typeof import('../../types').app;
declare const trigger: typeof import('../../types').trigger;
function registerHttpQuery(): void {
app.query('queryHandler', async (request, context) => {
context.log(request.method);
return { jsonBody: await request.json() };
});
app.query('queryOptions', {
route: 'search',
authLevel: 'function',
handler: async (request) => ({ body: await request.text() }),
});
app.http('queryMethods', {
methods: ['GET', 'QUERY'],
handler: () => ({ body: 'results' }),
});
trigger.http({ methods: ['QUERY'] });
}
void registerHttpQuery;

function registerServiceBusTopic(): void {
app.serviceBusTopic<string>('serviceBusTopic', serviceBusTopicFunctionOptions);
}
Expand Down
14 changes: 14 additions & 0 deletions types/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ export function post(name: string, handler: HttpHandler): void;
*/
export function post(name: string, options: HttpMethodFunctionOptions): void;

/**
* Registers an http function in your app that will be triggered by making a 'QUERY' request to the function url
* @param name The name of the function. This will be the route unless a route is explicitly configured in the `HttpTriggerOptions`
* @param handler The handler for this function
*/
export function query(name: string, handler: HttpHandler): void;

/**
* Registers an http function in your app that will be triggered by making a 'QUERY' request to the function url
* @param name The name of the function. This will be the route unless a route is explicitly configured in the `HttpTriggerOptions`
* @param options Configuration options describing the inputs, outputs, and handler for this function
*/
export function query(name: string, options: HttpMethodFunctionOptions): void;

/**
* Registers an http function in your app that will be triggered by making a 'PATCH' request to the function url
* @param name The name of the function. This will be the route unless a route is explicitly configured in the `HttpTriggerOptions`
Expand Down
12 changes: 11 additions & 1 deletion types/http.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,17 @@ export interface HttpRequestUser {
/**
* Possible values for an HTTP request method.
*/
export type HttpMethod = 'GET' | 'POST' | 'DELETE' | 'HEAD' | 'PATCH' | 'PUT' | 'OPTIONS' | 'TRACE' | 'CONNECT';
export type HttpMethod =
| 'GET'
| 'POST'
| 'DELETE'
| 'HEAD'
| 'PATCH'
| 'PUT'
| 'OPTIONS'
| 'TRACE'
| 'CONNECT'
| 'QUERY';

/**
* Possible values for an HTTP Request user type
Expand Down
Loading