Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Add HTTP request update property tests #1533

Merged
merged 25 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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: 0 additions & 4 deletions examples/motoko_examples/http_counter/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
"build": "npx azle http_counter",
"wasm": ".azle/http_counter/http_counter.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/http_counter",
"node_compatibility": true
},
"metadata": [
{
"name": "candid:service",
Expand Down
3 changes: 0 additions & 3 deletions examples/motoko_examples/http_counter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ export default Canister({
stableStorage.insert('counter', 0n);
}),
http_request: query([HttpRequest], HttpResponse(Token), (req) => {
console.log('Hello from http_request');

if (req.method === 'GET') {
if (req.headers.find(isGzip) === undefined) {
if (req.url === '/stream') {
Expand Down Expand Up @@ -161,7 +159,6 @@ export default Canister({
[Token],
StreamingCallbackHttpResponse(Token),
(token) => {
console.log('Hello from http_streaming');
switch (token.arbitrary_data) {
case 'start': {
return {
Expand Down
4 changes: 0 additions & 4 deletions examples/motoko_examples/http_counter/test/pretest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ async function pretest() {
execSync(`dfx deploy http_counter`, {
stdio: 'inherit'
});

execSync(`dfx generate http_counter`, {
stdio: 'inherit'
});
}

pretest();
3 changes: 1 addition & 2 deletions property_tests/arbitraries/http/response_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ export function HttpResponseValueArb<T>() {
return fc
.tuple(StatusCodeArb, HttpHeadersArb(), BodyArb())
.map(([status_code, headers, body]): HttpResponse<T> => {
const thing: HttpResponse<T> = {
return {
status_code,
headers,
body,
upgrade: None,
streaming_strategy: None
};
return thing;
});
}
export function HttpResponseArb<T extends CorrespondingJSType = any>(
Expand Down
108 changes: 0 additions & 108 deletions src/lib/canister_methods/http_gateway.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/lib/canister_methods/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './http_gateway';
export * from './methods';
export * from './types';
87 changes: 58 additions & 29 deletions src/lib/server.ts
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
blob,
bool,
CandidType,
Canister,
Func,
ic,
Expand All @@ -27,38 +28,65 @@ import { IncomingMessageForServer } from 'http';

const httpMessageParser = require('http-message-parser');

// TODO double-check all of these
export const Token = Record({
// add whatever fields you'd like
arbitrary_data: text
});

export const StreamingCallbackHttpResponse = Record({
body: blob,
token: Opt(Token)
});
export const DefaultToken = Record({});
export function StreamingCallbackHttpResponse<Token extends CandidType>(
token: Token
) {
return Record({
body: blob,
token: Opt(token)
});
}
type StreamingCallbackHttpResponse<Token> = {
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
bdemann marked this conversation as resolved.
Show resolved Hide resolved
body: Uint8Array;
token: Opt<Token>;
};

export const Callback = Func([text], StreamingCallbackHttpResponse, 'query');
function Callback<Token extends CandidType>(token: Token) {
bdemann marked this conversation as resolved.
Show resolved Hide resolved
return Func([token], Opt(StreamingCallbackHttpResponse(token)), 'query');
}
type Callback = Func;
lastmjs marked this conversation as resolved.
Show resolved Hide resolved

export const CallbackStrategy = Record({
callback: Callback,
token: Token
});
function CallbackStrategy<Token extends CandidType>(token: Token) {
return Record({
callback: Callback(token),
token
});
}
export type CallbackStrategy<Token> = {
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
callback: Callback;
token: Token;
};

function StreamingStrategy<Token extends CandidType>(token: Token) {
return Variant({
Callback: CallbackStrategy(token)
});
}

export const StreamingStrategy = Variant({
Callback: CallbackStrategy
});
export type StreamingStrategy<Token> = Variant<{
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
Callback: CallbackStrategy<Token>;
}>;

export type HeaderField = [text, text];
export const HeaderField = Tuple(text, text);

export const HttpResponse = Record({
status_code: nat16,
headers: Vec(HeaderField),
body: blob,
streaming_strategy: Opt(StreamingStrategy),
upgrade: Opt(bool)
});
export function HttpResponse<Token extends CandidType>(token?: Token) {
return Record({
status_code: nat16,
headers: Vec(HeaderField),
body: blob,
streaming_strategy: Opt(StreamingStrategy(token ?? DefaultToken)),
upgrade: Opt(bool)
});
}
export type HttpResponse<Token> = {
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
status_code: number;
headers: HeaderField[];
body: Uint8Array;
upgrade: Opt<boolean>;
streaming_strategy: Opt<StreamingStrategy<Token>>;
};

export const HttpRequest = Record({
method: text,
Expand All @@ -67,6 +95,7 @@ export const HttpRequest = Record({
body: blob,
certificate_version: Opt(nat16)
});
export type HttpRequest = typeof HttpRequest.tsType;
lastmjs marked this conversation as resolved.
Show resolved Hide resolved

let server: NodeServer;

Expand All @@ -80,7 +109,7 @@ export function Server(serverInit: () => NodeServer | Promise<NodeServer>) {
}),
http_request: query(
[HttpRequest],
Manual(HttpResponse),
Manual(HttpResponse()),
async (httpRequest) => {
await httpHandler(httpRequest, true);
},
Expand All @@ -90,7 +119,7 @@ export function Server(serverInit: () => NodeServer | Promise<NodeServer>) {
),
http_request_update: update(
[HttpRequest],
Manual(HttpResponse),
Manual(HttpResponse()),
async (httpRequest) => {
await httpHandler(httpRequest, false);
},
Expand Down Expand Up @@ -120,7 +149,7 @@ export async function httpHandler(
streaming_strategy: None,
upgrade: Some(true)
},
HttpResponse
HttpResponse()
);

return;
Expand Down Expand Up @@ -222,7 +251,7 @@ export async function httpHandler(
None: null
}
},
HttpResponse
HttpResponse()
);
}
}
Expand Down
Loading