Skip to content

Commit

Permalink
Merge branch 'main' into fetch_file
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs authored Mar 19, 2024
2 parents 890a6ed + ca0eeaf commit 4b5924d
Show file tree
Hide file tree
Showing 19 changed files with 655 additions and 274 deletions.
73 changes: 38 additions & 35 deletions examples/fetch_ic/src/backend/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export default Server(() => {
app.use(express.json());

app.get('/headers-array', (req, res) => {
res.setHeader('x-azle-response-0', 'x-azle-response-0');
res.setHeader('x-azle-response-1', 'x-azle-response-1');
res.setHeader('x-azle-response-2', 'x-azle-response-2');
res.setHeader('X-Azle-Response-Key-0', 'X-Azle-Response-Value-0');
res.setHeader('X-Azle-Response-Key-1', 'X-Azle-Response-Value-1');
res.setHeader('X-Azle-Response-Key-2', 'X-Azle-Response-Value-2');

res.json({
whoami: ic.caller().toString(),
Expand All @@ -18,9 +18,9 @@ export default Server(() => {
});

app.get('/headers-object', (req, res) => {
res.setHeader('x-azle-response-0', 'x-azle-response-0');
res.setHeader('x-azle-response-1', 'x-azle-response-1');
res.setHeader('x-azle-response-2', 'x-azle-response-2');
res.setHeader('X-Azle-Response-Key-0', 'X-Azle-Response-Value-0');
res.setHeader('X-Azle-Response-Key-1', 'X-Azle-Response-Value-1');
res.setHeader('X-Azle-Response-Key-2', 'X-Azle-Response-Value-2');

res.json({
whoami: ic.caller().toString(),
Expand All @@ -29,9 +29,9 @@ export default Server(() => {
});

app.post('/body-uint8array', (req, res) => {
res.setHeader('x-azle-response-0', 'x-azle-response-0');
res.setHeader('x-azle-response-1', 'x-azle-response-1');
res.setHeader('x-azle-response-2', 'x-azle-response-2');
res.setHeader('X-Azle-Response-Key-0', 'X-Azle-Response-Value-0');
res.setHeader('X-Azle-Response-Key-1', 'X-Azle-Response-Value-1');
res.setHeader('X-Azle-Response-Key-2', 'X-Azle-Response-Value-2');

res.json({
whoami: ic.caller().toString(),
Expand All @@ -40,9 +40,9 @@ export default Server(() => {
});

app.put('/body-string', (req, res) => {
res.setHeader('x-azle-response-0', 'x-azle-response-0');
res.setHeader('x-azle-response-1', 'x-azle-response-1');
res.setHeader('x-azle-response-2', 'x-azle-response-2');
res.setHeader('X-Azle-Response-Key-0', 'X-Azle-Response-Value-0');
res.setHeader('X-Azle-Response-Key-1', 'X-Azle-Response-Value-1');
res.setHeader('X-Azle-Response-Key-2', 'X-Azle-Response-Value-2');

res.json({
whoami: ic.caller().toString(),
Expand All @@ -51,9 +51,9 @@ export default Server(() => {
});

app.patch('/body-array-buffer', (req, res) => {
res.setHeader('x-azle-response-0', 'x-azle-response-0');
res.setHeader('x-azle-response-1', 'x-azle-response-1');
res.setHeader('x-azle-response-2', 'x-azle-response-2');
res.setHeader('X-Azle-Response-Key-0', 'X-Azle-Response-Value-0');
res.setHeader('X-Azle-Response-Key-1', 'X-Azle-Response-Value-1');
res.setHeader('X-Azle-Response-Key-2', 'X-Azle-Response-Value-2');

res.json({
whoami: ic.caller().toString(),
Expand All @@ -62,9 +62,9 @@ export default Server(() => {
});

app.delete('/body-blob', (req, res) => {
res.setHeader('x-azle-response-0', 'x-azle-response-0');
res.setHeader('x-azle-response-1', 'x-azle-response-1');
res.setHeader('x-azle-response-2', 'x-azle-response-2');
res.setHeader('X-Azle-Response-Key-0', 'X-Azle-Response-Value-0');
res.setHeader('X-Azle-Response-Key-1', 'X-Azle-Response-Value-1');
res.setHeader('X-Azle-Response-Key-2', 'X-Azle-Response-Value-2');

res.json({
whoami: ic.caller().toString(),
Expand All @@ -73,9 +73,9 @@ export default Server(() => {
});

app.post('/body-data-view', (req, res) => {
res.setHeader('x-azle-response-0', 'x-azle-response-0');
res.setHeader('x-azle-response-1', 'x-azle-response-1');
res.setHeader('x-azle-response-2', 'x-azle-response-2');
res.setHeader('X-Azle-Response-Key-0', 'X-Azle-Response-Value-0');
res.setHeader('X-Azle-Response-Key-1', 'X-Azle-Response-Value-1');
res.setHeader('X-Azle-Response-Key-2', 'X-Azle-Response-Value-2');

res.json({
whoami: ic.caller().toString(),
Expand All @@ -84,9 +84,9 @@ export default Server(() => {
});

app.get('/url-query-params-get', (req, res) => {
res.setHeader('x-azle-response-0', 'x-azle-response-0');
res.setHeader('x-azle-response-1', 'x-azle-response-1');
res.setHeader('x-azle-response-2', 'x-azle-response-2');
res.setHeader('X-Azle-Response-Key-0', 'X-Azle-Response-Value-0');
res.setHeader('X-Azle-Response-Key-1', 'X-Azle-Response-Value-1');
res.setHeader('X-Azle-Response-Key-2', 'X-Azle-Response-Value-2');

res.json({
whoami: ic.caller().toString(),
Expand All @@ -95,33 +95,36 @@ export default Server(() => {
});

app.post('/url-query-params-post', (req, res) => {
res.setHeader('x-azle-response-0', 'x-azle-response-0');
res.setHeader('x-azle-response-1', 'x-azle-response-1');
res.setHeader('x-azle-response-2', 'x-azle-response-2');
res.setHeader('X-Azle-Response-Key-0', 'X-Azle-Response-Value-0');
res.setHeader('X-Azle-Response-Key-1', 'X-Azle-Response-Value-1');
res.setHeader('X-Azle-Response-Key-2', 'X-Azle-Response-Value-2');

res.json({
whoami: ic.caller().toString(),
value: req.query
});
});

app.get('/not-authorized-get', (req, res) => {
app.get('/not-authorized-get', (_req, res) => {
res.status(401).send('Not Authorized');
});

app.post('/not-authorized-post', (req, res) => {
app.post('/not-authorized-post', (_req, res) => {
res.status(401).send('Not Authorized');
});

app.head('/head', (req, res) => {
res.setHeader('x-azle-response-0', 'x-azle-response-0');
res.setHeader('x-azle-response-1', 'x-azle-response-1');
res.setHeader('x-azle-response-2', 'x-azle-response-2');
app.head('/head', (_req, res) => {
res.setHeader('X-Azle-Response-Key-0', 'X-Azle-Response-Value-0');
res.setHeader('X-Azle-Response-Key-1', 'X-Azle-Response-Value-1');
res.setHeader('X-Azle-Response-Key-2', 'X-Azle-Response-Value-2');
res.end();
});

app.options('/options', (req, res) => {
res.setHeader('x-azle-response-options', 'x-azle-response-options');
app.options('/options', (_req, res) => {
res.setHeader(
'X-Azle-Response-Key-Options',
'X-Azle-Response-Value-Options'
);
res.end();
});

Expand Down
124 changes: 80 additions & 44 deletions examples/fetch_ic/src/frontend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,28 @@ export class AzleApp extends LitElement {
method: 'GET',
headers: [
['Authorization', toJwt(this.identity)],
['x-azle-request-0', 'x-azle-request-0'],
['x-azle-request-1', 'x-azle-request-1'],
['x-azle-request-2', 'x-azle-request-2']
['X-Azle-Request-Key-0', 'X-Azle-Request-Value-0'],
['X-Azle-Request-Key-1', 'X-Azle-Request-Value-1'],
['X-Azle-Request-Key-2', 'X-Azle-Request-Value-2']
]
}
);
const responseJson = await response.json();

if (
responseJson.whoami === this.identity.getPrincipal().toString() &&
responseJson.value['x-azle-request-0'] === 'x-azle-request-0' &&
responseJson.value['x-azle-request-1'] === 'x-azle-request-1' &&
responseJson.value['x-azle-request-2'] === 'x-azle-request-2' &&
response.headers.get('x-azle-response-0') === 'x-azle-response-0' &&
response.headers.get('x-azle-response-1') === 'x-azle-response-1' &&
response.headers.get('x-azle-response-2') === 'x-azle-response-2'
responseJson.value['X-Azle-Request-Key-0'.toLowerCase()] ===
'X-Azle-Request-Value-0' &&
responseJson.value['X-Azle-Request-Key-1'.toLowerCase()] ===
'X-Azle-Request-Value-1' &&
responseJson.value['X-Azle-Request-Key-2'.toLowerCase()] ===
'X-Azle-Request-Value-2' &&
response.headers.get('X-Azle-Response-Key-0') ===
'X-Azle-Response-Value-0' &&
response.headers.get('X-Azle-Response-Key-1') ===
'X-Azle-Response-Value-1' &&
response.headers.get('X-Azle-Response-Key-2') ===
'X-Azle-Response-Value-2'
) {
(window as any).headersArraySuccess = true;
}
Expand All @@ -85,22 +91,28 @@ export class AzleApp extends LitElement {
method: 'GET',
headers: {
Authorization: toJwt(this.identity),
'x-azle-request-0': 'x-azle-request-0',
'x-azle-request-1': 'x-azle-request-1',
'x-azle-request-2': 'x-azle-request-2'
'X-Azle-Request-Key-0': 'X-Azle-Request-Value-0',
'X-Azle-Request-Key-1': 'X-Azle-Request-Value-1',
'X-Azle-Request-Key-2': 'X-Azle-Request-Value-2'
}
}
);
const responseJson = await response.json();

if (
responseJson.whoami === this.identity.getPrincipal().toString() &&
responseJson.value['x-azle-request-0'] === 'x-azle-request-0' &&
responseJson.value['x-azle-request-1'] === 'x-azle-request-1' &&
responseJson.value['x-azle-request-2'] === 'x-azle-request-2' &&
response.headers.get('x-azle-response-0') === 'x-azle-response-0' &&
response.headers.get('x-azle-response-1') === 'x-azle-response-1' &&
response.headers.get('x-azle-response-2') === 'x-azle-response-2'
responseJson.value['X-Azle-Request-Key-0'.toLowerCase()] ===
'X-Azle-Request-Value-0' &&
responseJson.value['X-Azle-Request-Key-1'.toLowerCase()] ===
'X-Azle-Request-Value-1' &&
responseJson.value['X-Azle-Request-Key-2'.toLowerCase()] ===
'X-Azle-Request-Value-2' &&
response.headers.get('X-Azle-Response-Key-0') ===
'X-Azle-Response-Value-0' &&
response.headers.get('X-Azle-Response-Key-1') ===
'X-Azle-Response-Value-1' &&
response.headers.get('X-Azle-Response-Key-2') ===
'X-Azle-Response-Value-2'
) {
(window as any).headersObjectSuccess = true;
}
Expand Down Expand Up @@ -137,9 +149,12 @@ export class AzleApp extends LitElement {
JSON.stringify({
value: 'body-uint8array'
}) &&
response.headers.get('x-azle-response-0') === 'x-azle-response-0' &&
response.headers.get('x-azle-response-1') === 'x-azle-response-1' &&
response.headers.get('x-azle-response-2') === 'x-azle-response-2'
response.headers.get('X-Azle-Response-Key-0') ===
'X-Azle-Response-Value-0' &&
response.headers.get('X-Azle-Response-Key-1') ===
'X-Azle-Response-Value-1' &&
response.headers.get('X-Azle-Response-Key-2') ===
'X-Azle-Response-Value-2'
) {
(window as any).bodyUint8ArraySuccess = true;
}
Expand Down Expand Up @@ -171,9 +186,12 @@ export class AzleApp extends LitElement {
JSON.stringify({
value: 'body-string'
}) &&
response.headers.get('x-azle-response-0') === 'x-azle-response-0' &&
response.headers.get('x-azle-response-1') === 'x-azle-response-1' &&
response.headers.get('x-azle-response-2') === 'x-azle-response-2'
response.headers.get('X-Azle-Response-Key-0') ===
'X-Azle-Response-Value-0' &&
response.headers.get('X-Azle-Response-Key-1') ===
'X-Azle-Response-Value-1' &&
response.headers.get('X-Azle-Response-Key-2') ===
'X-Azle-Response-Value-2'
) {
(window as any).bodyStringSuccess = true;
}
Expand Down Expand Up @@ -210,9 +228,12 @@ export class AzleApp extends LitElement {
JSON.stringify({
value: 'body-array-buffer'
}) &&
response.headers.get('x-azle-response-0') === 'x-azle-response-0' &&
response.headers.get('x-azle-response-1') === 'x-azle-response-1' &&
response.headers.get('x-azle-response-2') === 'x-azle-response-2'
response.headers.get('X-Azle-Response-Key-0') ===
'X-Azle-Response-Value-0' &&
response.headers.get('X-Azle-Response-Key-1') ===
'X-Azle-Response-Value-1' &&
response.headers.get('X-Azle-Response-Key-2') ===
'X-Azle-Response-Value-2'
) {
(window as any).bodyArrayBufferSuccess = true;
}
Expand Down Expand Up @@ -251,9 +272,12 @@ export class AzleApp extends LitElement {
JSON.stringify({
value: 'body-blob'
}) &&
response.headers.get('x-azle-response-0') === 'x-azle-response-0' &&
response.headers.get('x-azle-response-1') === 'x-azle-response-1' &&
response.headers.get('x-azle-response-2') === 'x-azle-response-2'
response.headers.get('X-Azle-Response-Key-0') ===
'X-Azle-Response-Value-0' &&
response.headers.get('X-Azle-Response-Key-1') ===
'X-Azle-Response-Value-1' &&
response.headers.get('X-Azle-Response-Key-2') ===
'X-Azle-Response-Value-2'
) {
(window as any).bodyBlobSuccess = true;
}
Expand Down Expand Up @@ -292,9 +316,12 @@ export class AzleApp extends LitElement {
JSON.stringify({
value: 'body-data-view'
}) &&
response.headers.get('x-azle-response-0') === 'x-azle-response-0' &&
response.headers.get('x-azle-response-1') === 'x-azle-response-1' &&
response.headers.get('x-azle-response-2') === 'x-azle-response-2'
response.headers.get('X-Azle-Response-Key-0') ===
'X-Azle-Response-Value-0' &&
response.headers.get('X-Azle-Response-Key-1') ===
'X-Azle-Response-Value-1' &&
response.headers.get('X-Azle-Response-Key-2') ===
'X-Azle-Response-Value-2'
) {
(window as any).bodyDataViewSuccess = true;
}
Expand All @@ -318,9 +345,12 @@ export class AzleApp extends LitElement {
if (
responseJson.whoami === this.identity.getPrincipal().toString() &&
responseJson.value.type === 'get' &&
response.headers.get('x-azle-response-0') === 'x-azle-response-0' &&
response.headers.get('x-azle-response-1') === 'x-azle-response-1' &&
response.headers.get('x-azle-response-2') === 'x-azle-response-2'
response.headers.get('X-Azle-Response-Key-0') ===
'X-Azle-Response-Value-0' &&
response.headers.get('X-Azle-Response-Key-1') ===
'X-Azle-Response-Value-1' &&
response.headers.get('X-Azle-Response-Key-2') ===
'X-Azle-Response-Value-2'
) {
(window as any).urlQueryParamsGetSuccess = true;
}
Expand All @@ -345,9 +375,12 @@ export class AzleApp extends LitElement {
if (
responseJson.whoami === this.identity.getPrincipal().toString() &&
responseJson.value.type === 'post' &&
response.headers.get('x-azle-response-0') === 'x-azle-response-0' &&
response.headers.get('x-azle-response-1') === 'x-azle-response-1' &&
response.headers.get('x-azle-response-2') === 'x-azle-response-2'
response.headers.get('X-Azle-Response-Key-0') ===
'X-Azle-Response-Value-0' &&
response.headers.get('X-Azle-Response-Key-1') ===
'X-Azle-Response-Value-1' &&
response.headers.get('X-Azle-Response-Key-2') ===
'X-Azle-Response-Value-2'
) {
(window as any).urlQueryParamsPostSuccess = true;
}
Expand Down Expand Up @@ -390,9 +423,12 @@ export class AzleApp extends LitElement {
);

if (
response.headers.get('x-azle-response-0') === 'x-azle-response-0' &&
response.headers.get('x-azle-response-1') === 'x-azle-response-1' &&
response.headers.get('x-azle-response-2') === 'x-azle-response-2'
response.headers.get('X-Azle-Response-Key-0') ===
'X-Azle-Response-Value-0' &&
response.headers.get('X-Azle-Response-Key-1') ===
'X-Azle-Response-Value-1' &&
response.headers.get('X-Azle-Response-Key-2') ===
'X-Azle-Response-Value-2'
) {
(window as any).headSuccess = true;
}
Expand All @@ -408,8 +444,8 @@ export class AzleApp extends LitElement {
);

if (
response.headers.get('x-azle-response-options') ===
'x-azle-response-options'
response.headers.get('X-Azle-Response-Key-Options') ===
'X-Azle-Response-Value-Options'
) {
(window as any).optionsSuccess = true;
}
Expand Down
5 changes: 2 additions & 3 deletions examples/fetch_ic/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import * as dns from 'node:dns';
dns.setDefaultResultOrder('ipv4first');

import { Principal } from '@dfinity/principal'; // TODO get rid of this dependency if we installed it
import { Test, getCanisterOrigin } from 'azle/test';
import puppeteer, { Browser, Page } from 'puppeteer';

Expand All @@ -28,7 +27,7 @@ export function getTests(canisterName: string): Test[] {

page.on('console', (message) => {
for (const arg of message.args()) {
console.log(`Puppetteer log: ${arg}`);
console.info(`Puppetteer log: ${arg}`);
}
});

Expand Down Expand Up @@ -283,7 +282,7 @@ export function getTests(canisterName: string): Test[] {
];
}

function iiLogin(page: Page) {
function iiLogin(page: Page): Promise<void> {
return new Promise<void>((resolve) => {
page.once('popup', async (iiPage) => {
if (iiPage === null) {
Expand Down
Loading

0 comments on commit 4b5924d

Please sign in to comment.