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

feat: add createHandler() #622

Merged
merged 5 commits into from
May 30, 2023
Merged

Conversation

Octo8080X
Copy link
Contributor

I cut out the function that gets an hundler from fresh.

The purpose is to prepare you to create tests for your application.
The introduction of this feature does not affect fresh.

Example:

/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
/// <reference lib="deno.unstable" />

import { createHandler } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";

import { superdeno } from "https://deno.land/x/superdeno/mod.ts";
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import {
  type RequestHandlerLike,
} from "https://deno.land/x/superdeno/src/types.ts";
import {
  Document,
  DOMParser,
} from "https://deno.land/x/deno_dom/deno-dom-wasm.ts";


Deno.test("HTTP assert test.", async (t) => {
  await t.step("#1 GET /", async () => {
    const handler = await createHandler(manifest) as RequestHandlerLike;

    await superdeno(handler)
      .get("/")
      .expect(200);
  });
  await t.step("#2 GET /Foo", async () => {
    const server = await createHandler(manifest) as RequestHandlerLike;

    const r = await superdeno(server)
      .get("/Foo")
      .expect(200);

    const document: Document = new DOMParser().parseFromString(
      r.text,
      "text/html",
    )!;

    assertEquals(document.querySelector("div")?.innerText, "Hello Foo");
  });

  await t.step("#3 GET /Foo/Bar", async () => {
    const server = await createHandler(manifest) as RequestHandlerLike;

    await superdeno(server)
      .get("/Foo/Bar")
      .expect(404);
  });
});
$ deno test -A main_test.ts
Check file:///usr/src/app/my-project/main_test.ts
running 1 test from ./main_test.ts
HTTP assert test. ...
  #1 GET / ... ok (53ms)
  #2 GET /Foo ... ok (50ms)
  #3 GET /Foo/Bar ... ok (45ms)
HTTP assert test. ... ok (154ms)

ok | 1 passed (3 steps) | 0 failed (300ms)

This was discussed in a separate pull request, but prepared in a separate branch based on @lucacasonato suggestion.

Add a function to get a server class instance.

Thenk you.

@Octo8080X
Copy link
Contributor Author

Octo8080X commented Sep 26, 2022

Hi. @lucacasonato

I have merged Fresh 1.1.1 and verified that it works.
If everything is OK, please approve.

Thank you very much.

@iuioiua iuioiua changed the title Add a function to create handler. feat: add createHandler() May 30, 2023
Copy link
Contributor

@iuioiua iuioiua left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool and LGTM! Let's add an example to the documentation to show devs how to use this in a follow-up PR. I'll create an issue.

@iuioiua iuioiua merged commit 675036b into denoland:main May 30, 2023
iuioiua added a commit that referenced this pull request Jun 12, 2023
Fixes [#1222](#1222)


Add example implementation and documentation of test code using
`createHandler()` newly added by
[#622](#622)

---------

Co-authored-by: Asher Gomez <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants