Last active
May 4, 2023 07:30
-
-
Save kuc-arc-f/785592df338bbf1338f092f300606b32 to your computer and use it in GitHub Desktop.
cloudflare D1, workers HTTP test
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
const assert = require('assert') | |
const fetch = require('node-fetch') | |
const PUBLIC_API_URL = "http://localhost:8787"; | |
const PUBLIC_API_KEY = "hoge123"; | |
// | |
async function main () { | |
try { | |
const newTodo = { | |
api_key: PUBLIC_API_KEY, | |
title: 'Buy groceries', | |
content: 'Milk, bread, eggs', | |
completed: 0 | |
}; | |
const options = { | |
method: 'POST', | |
body: JSON.stringify(newTodo), | |
headers: { 'Content-Type': 'application/json' }, | |
}; | |
const response = await fetch(PUBLIC_API_URL + "/todos/create", options) | |
const data = await response.json(); | |
console.log(data); | |
assert.strictEqual(response.status, 200) | |
//expect(data.title).toBe('Buy groceries'); | |
assert.strictEqual(data.data.title, newTodo.title); | |
assert.strictEqual(data.data.content, newTodo.content); | |
assert.strictEqual(data.data.completed, newTodo.completed); | |
} catch (err) { | |
console.error(err) | |
} | |
} | |
// | |
main(); |
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
{ | |
"name": "d1-project5test", | |
"version": "1.0.0", | |
"description": "", | |
"main": "test1.js", | |
"directories": { | |
"doc": "doc" | |
}, | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"devDependencies": { | |
"node-fetch": "^2.6.9" | |
} | |
} |
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
const assert = require('assert') | |
const fetch = require('node-fetch') | |
const PUBLIC_API_URL = "http://localhost:8787"; | |
const PUBLIC_API_KEY = "hoge123"; | |
// | |
const newTodo = { | |
api_key: PUBLIC_API_KEY, | |
title: 'Buy groceries', | |
content: 'Milk, bread, eggs', | |
completed: 0, | |
id: 0, | |
}; | |
// | |
async function createTodo () { | |
const options = { | |
method: 'POST', | |
body: JSON.stringify(newTodo), | |
headers: { 'Content-Type': 'application/json' }, | |
}; | |
const response = await fetch(PUBLIC_API_URL + "/todos/create", options) | |
const data = await response.json(); | |
console.log(data); | |
//id | |
newTodo.id = data.data.id; | |
assert.strictEqual(response.status, 200) | |
//expect(data.title).toBe('Buy groceries'); | |
assert.strictEqual(data.data.title, newTodo.title); | |
assert.strictEqual(data.data.content, newTodo.content); | |
assert.strictEqual(data.data.completed, newTodo.completed); | |
} | |
// | |
async function main () { | |
try { | |
await createTodo(); | |
console.log("#start_get") | |
console.log(newTodo) | |
const options = { | |
method: 'POST', | |
body: JSON.stringify(newTodo), | |
headers: { 'Content-Type': 'application/json' }, | |
}; | |
const response = await fetch(PUBLIC_API_URL + "/todos/get", options) | |
const data = await response.json(); | |
console.log(data); | |
assert.strictEqual(data.data.id, newTodo.id); | |
assert.strictEqual(data.data.title, newTodo.title); | |
assert.strictEqual(data.data.content, newTodo.content); | |
assert.strictEqual(data.data.completed, newTodo.completed); | |
} catch (err) { | |
console.error(err) | |
} | |
} | |
// | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment