[#3447] added jsvm http.Cookie binding

This commit is contained in:
Gani Georgiev 2023-10-07 15:35:20 +03:00
parent 6d672348e7
commit 49e3f4ad93
4 changed files with 3034 additions and 2931 deletions

View File

@ -412,6 +412,11 @@ func baseBinds(vm *goja.Runtime) {
return instanceValue
})
vm.Set("Cookie", func(call goja.ConstructorCall) *goja.Object {
instance := &http.Cookie{}
return structConstructor(vm, call, instance)
})
}
func dbxBinds(vm *goja.Runtime) {

View File

@ -46,7 +46,7 @@ func TestBaseBindsCount(t *testing.T) {
vm := goja.New()
baseBinds(vm)
testBindsCount(vm, "this", 14, t)
testBindsCount(vm, "this", 15, t)
}
func TestBaseBindsReaderToString(t *testing.T) {
@ -69,6 +69,38 @@ func TestBaseBindsReaderToString(t *testing.T) {
}
}
func TestBaseBindsCookie(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
vm := goja.New()
baseBinds(vm)
_, err := vm.RunString(`
const cookie = new Cookie({
name: "example_name",
value: "example_value",
path: "/example_path",
domain: "example.com",
maxAge: 10,
secure: true,
httpOnly: true,
sameSite: 3,
});
const result = cookie.string();
const expected = "example_name=example_value; Path=/example_path; Domain=example.com; Max-Age=10; HttpOnly; Secure; SameSite=Strict";
if (expected != result) {
throw new("Expected \n" + expected + "\ngot\n" + result);
}
`)
if err != nil {
t.Fatal(err)
}
}
func TestBaseBindsRecord(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()

File diff suppressed because it is too large Load Diff

View File

@ -450,6 +450,36 @@ declare class Dao implements daos.Dao {
constructor(concurrentDB?: dbx.Builder, nonconcurrentDB?: dbx.Builder)
}
interface Cookie extends http.Cookie{} // merge
/**
* A Cookie represents an HTTP cookie as sent in the Set-Cookie header of an
* HTTP response.
*
* Example:
*
* ` + "```" + `js
* routerAdd("POST", "/example", (c) => {
* c.setCookie(new Cookie({
* name: "example_name",
* value: "example_value",
* path: "/",
* domain: "example.com",
* maxAge: 10,
* secure: true,
* httpOnly: true,
* sameSite: 3,
* }))
*
* return c.redirect(200, "/");
* })
* ` + "```" + `
*
* @group PocketBase
*/
declare class Cookie implements http.Cookie {
constructor(options?: Partial<http.Cookie>)
}
// -------------------------------------------------------------------
// dbxBinds
// -------------------------------------------------------------------