-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change to fetch for the property tests
- Loading branch information
Showing
6 changed files
with
95 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 26 additions & 42 deletions
68
property_tests/tests/canister_methods/http_request/test/fletch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,40 @@ | ||
import { execSync } from 'child_process'; | ||
import { HttpRequest } from 'azle'; | ||
import { getCanisterId } from 'azle/test'; | ||
import * as dns from 'node:dns'; | ||
dns.setDefaultResultOrder('ipv4first'); | ||
|
||
type HttpResponse = { | ||
status: number; | ||
headers: string[][]; | ||
body: string; | ||
}; | ||
|
||
/** | ||
* A synchronous "fetch" for canisters. | ||
* An asynchronous "fetch" for canisters. | ||
*/ | ||
export function fletch(canisterName: string, options: HttpRequest) { | ||
export async function fletch( | ||
canisterName: string, | ||
request: HttpRequest | ||
): Promise<HttpResponse> { | ||
const canisterId = getCanisterId(canisterName); | ||
|
||
const requestHeaders = toCurlHeadersString(options.headers); | ||
|
||
const curlCommand = `curl\ | ||
--silent\ | ||
--include\ | ||
-X ${options.method}\ | ||
${requestHeaders}\ | ||
--data "${options.body.join(',')}"\ | ||
"${canisterId}.localhost:8000${options.url}" \ | ||
--resolve "${canisterId}.localhost:8000:127.0.0.1"`; | ||
const { method, body, headers, url: path } = request; | ||
|
||
const responseBuffer = execSync(curlCommand); | ||
|
||
return toResponse(responseBuffer); | ||
} | ||
const url = `http://${canisterId}.localhost:8000${path}`; | ||
|
||
function toCurlHeadersString(headers: [string, string][]) { | ||
return headers | ||
.map(([name, value]) => `-H ${singleQuotedString(`${name}: ${value}`)}`) | ||
.join(' '); | ||
} | ||
|
||
function singleQuotedString(input: string) { | ||
const singleQuoteEscapedString = input.replace(/'/g, "'\\''"); | ||
const fetchOptions = { | ||
method, | ||
headers, | ||
body: method === 'GET' ? undefined : body | ||
}; | ||
|
||
return `'${singleQuoteEscapedString}'`; | ||
return await toResponse(await fetch(url, fetchOptions)); | ||
} | ||
|
||
function toResponse(buffer: Buffer) { | ||
const responseString = new TextDecoder().decode(buffer); | ||
|
||
const [statusCodeAndHeaders, body] = responseString.split('\r\n\r\n'); | ||
|
||
const [statusCodeString, ...responseHeaderStrings] = | ||
statusCodeAndHeaders.split('\r\n'); | ||
|
||
const status = Number(statusCodeString.split(' ')[1]); | ||
async function toResponse(response: Response): Promise<HttpResponse> { | ||
const headers: [string, string][] = Array.from(response.headers.entries()); | ||
const status = response.status; | ||
const body = await response.text(); | ||
|
||
const headers = responseHeaderStrings.map((header) => header.split(': ')); | ||
|
||
return { | ||
status, | ||
headers, | ||
body | ||
}; | ||
return { status, headers, body }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 26 additions & 42 deletions
68
property_tests/tests/canister_methods/http_request_update/test/fletch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,40 @@ | ||
import { execSync } from 'child_process'; | ||
import { HttpRequest } from 'azle'; | ||
import { getCanisterId } from 'azle/test'; | ||
import * as dns from 'node:dns'; | ||
dns.setDefaultResultOrder('ipv4first'); | ||
|
||
type HttpResponse = { | ||
status: number; | ||
headers: string[][]; | ||
body: string; | ||
}; | ||
|
||
/** | ||
* A synchronous "fetch" for canisters. | ||
* An asynchronous "fetch" for canisters. | ||
*/ | ||
export function fletch(canisterName: string, options: HttpRequest) { | ||
export async function fletch( | ||
canisterName: string, | ||
request: HttpRequest | ||
): Promise<HttpResponse> { | ||
const canisterId = getCanisterId(canisterName); | ||
|
||
const requestHeaders = toCurlHeadersString(options.headers); | ||
|
||
const curlCommand = `curl\ | ||
--silent\ | ||
--include\ | ||
-X ${options.method}\ | ||
${requestHeaders}\ | ||
--data "${options.body.join(',')}"\ | ||
"${canisterId}.localhost:8000${options.url}" \ | ||
--resolve "${canisterId}.localhost:8000:127.0.0.1"`; | ||
const { method, body, headers, url: path } = request; | ||
|
||
const responseBuffer = execSync(curlCommand); | ||
|
||
return toResponse(responseBuffer); | ||
} | ||
const url = `http://${canisterId}.localhost:8000${path}`; | ||
|
||
function toCurlHeadersString(headers: [string, string][]) { | ||
return headers | ||
.map(([name, value]) => `-H ${singleQuotedString(`${name}: ${value}`)}`) | ||
.join(' '); | ||
} | ||
|
||
function singleQuotedString(input: string) { | ||
const singleQuoteEscapedString = input.replace(/'/g, "'\\''"); | ||
const fetchOptions = { | ||
method, | ||
headers, | ||
body: method === 'GET' ? undefined : body | ||
}; | ||
|
||
return `'${singleQuoteEscapedString}'`; | ||
return await toResponse(await fetch(url, fetchOptions)); | ||
} | ||
|
||
function toResponse(buffer: Buffer) { | ||
const responseString = new TextDecoder().decode(buffer); | ||
|
||
const [statusCodeAndHeaders, body] = responseString.split('\r\n\r\n'); | ||
|
||
const [statusCodeString, ...responseHeaderStrings] = | ||
statusCodeAndHeaders.split('\r\n'); | ||
|
||
const status = Number(statusCodeString.split(' ')[1]); | ||
async function toResponse(response: Response): Promise<HttpResponse> { | ||
const headers: [string, string][] = Array.from(response.headers.entries()); | ||
const status = response.status; | ||
const body = await response.text(); | ||
|
||
const headers = responseHeaderStrings.map((header) => header.split(': ')); | ||
|
||
return { | ||
status, | ||
headers, | ||
body | ||
}; | ||
return { status, headers, body }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters