Skip to content

Commit

Permalink
feat: 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
asos-craigmorten committed May 31, 2020
1 parent f080fec commit ecc3da4
Show file tree
Hide file tree
Showing 28 changed files with 976 additions and 434 deletions.
4 changes: 4 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# ChangeLog

## [1.0.0] - 31-05-2020

- feat: initial port of supertest.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# CODEOWNERS

- @asos-craigmorten
- @asos-craigmorten - <craig.morten@asos.com>
128 changes: 128 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Contributor Covenant Code of Conduct

## Our Pledge

We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, religion, or sexual identity
and orientation.

We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.

## Our Standards

Examples of behavior that contributes to a positive environment for our
community include:

- Demonstrating empathy and kindness toward other people
- Being respectful of differing opinions, viewpoints, and experiences
- Giving and gracefully accepting constructive feedback
- Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
- Focusing on what is best not just for us as individuals, but for the
overall community

Examples of unacceptable behavior include:

- The use of sexualized language or imagery, and sexual attention or
advances of any kind
- Trolling, insulting or derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information, such as a physical or email
address, without their explicit permission
- Other conduct which could reasonably be considered inappropriate in a
professional setting

## Enforcement Responsibilities

Community leaders are responsible for clarifying and enforcing our standards of
acceptable behavior and will take appropriate and fair corrective action in
response to any behavior that they deem inappropriate, threatening, offensive,
or harmful.

Community leaders have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, and will communicate reasons for moderation
decisions when appropriate.

## Scope

This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
<craig.morten@asos.com>. All complaints will be reviewed and investigated promptly
and fairly.

All community leaders are obligated to respect the privacy and security of the
reporter of any incident.

## Enforcement Guidelines

Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct:

### 1. Correction

**Community Impact**: Use of inappropriate language or other behavior deemed
unprofessional or unwelcome in the community.

**Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the
behavior was inappropriate. A public apology may be requested.

### 2. Warning

**Community Impact**: A violation through a single incident or series
of actions.

**Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or
permanent ban.

### 3. Temporary Ban

**Community Impact**: A serious violation of community standards, including
sustained inappropriate behavior.

**Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban.

### 4. Permanent Ban

**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals.

**Consequence**: A permanent ban from any sort of public interaction within
the community.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.0, available at
<https://www.contributor-covenant.org/version/2/0/code_of_conduct.html>.

Community Impact Guidelines were inspired by [Mozilla's code of conduct
enforcement ladder](https://github.com/mozilla/diversity).

[homepage]: https://www.contributor-covenant.org

For answers to common questions about this code of conduct, see the FAQ at
<https://www.contributor-covenant.org/faq>. Translations are available at
<https://www.contributor-covenant.org/translations>.
185 changes: 183 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,190 @@

HTTP assertions for Deno made easy via [superagent](https://github.com/visionmedia/superagent).

![Test](https://github.com/asos-craigmorten/superdeno/workflows/Test/badge.svg) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) [![](https://img.shields.io/github/issues/asos-craigmorten/superdeno)](https://img.shields.io/github/issues/asos-craigmorten/superdeno)
[![GitHub tag](https://img.shields.io/github/tag/asos-craigmorten/superdeno)](https://github.com/asos-craigmorten/superdeno/tags/) ![Test](https://github.com/asos-craigmorten/superdeno/workflows/Test/badge.svg) [![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/superdeno/mod.ts) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) [![](https://img.shields.io/github/issues/asos-craigmorten/superdeno)](https://img.shields.io/github/issues/asos-craigmorten/superdeno)
![](https://img.shields.io/github/stars/asos-craigmorten/superdeno) ![](https://img.shields.io/github/forks/asos-craigmorten/superdeno) ![](https://img.shields.io/github/license/asos-craigmorten/superdeno) [![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://GitHub.com/asos-craigmorten/superdeno/graphs/commit-activity) [![HitCount](http://hits.dwyl.com/asos-craigmorten/superdeno.svg)](http://hits.dwyl.com/asos-craigmorten/superdeno)

```ts
import { superdeno } from "https://deno.land/x/superdeno@master/mod.ts";
import { superdeno } from "https://deno.land/x/superdeno@master/mod.ts";

const app = superdeno();

app.get("/user", (req, res) => {
res.setStatus(200).json({ name: "deno" });
});

request(app)
.get("/user")
.expect("Content-Type", /json/)
.expect("Content-Length", "15")
.expect(200)
.end((err, res) => {
if (err) throw err;
});
```

## About

The motivation of this module is to provide a high-level abstraction for testing HTTP in Deno, while still allowing you to drop down to the lower-level API provided by [superagent](https://visionmedia.github.io/superagent/).

## Installation

This is a [Deno](https://deno.land/) module available to import direct from this repo and via the [Deno Registry](https://deno.land/x).

Before importing, [download and install Deno](https://deno.land/#installation).

You can then import SuperDeno straight into your project:

```ts
import { superdeno } from "https://deno.land/x/superdeno@master/mod.ts";
```

If you want to use a specific version of SuperDeno, just modify the import url to contain the version:

```ts
import { superdeno } from "https://deno.land/x/superdeno@0.3.0/mod.ts";
```

Or if you want to use a specific commit of SuperDeno, just modify the import url to contain the commit hash:

```ts
import { superdeno } from "https://deno.land/x/superdeno@c21f8d6/mod.ts";
```

## Example

You may pass a url string, [`http.Server`](https://doc.deno.land/https/deno.land/std/http/mod.ts#Server), a request handling function, or an object that implements an `app.listen()` method (which mirrors the [`http.serve`](https://doc.deno.land/https/deno.land/std/http/mod.ts#serve) interface) to `superdeno()` - if SuperDeno identifies that a server is not already listening for connections, then one is bound to an ephemeral port for you so there is no need to keep track of ports.

SuperDeno works with any Deno test framework. Here's an example with Deno's built-in test framework, note how you can pass `done` straight to any of the `.expect()` calls:

```ts
Deno.test("GET /user responds with json", (done) => {
request(app)
.get("/user")
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(200, done);
});
```

Here's an example of SuperDeno working with the Opine web framework:

```ts
import { opine } from "https://deno.land/x/opine@0.8.0/mod.ts";
import { expect } from "https://deno.land/x/expect@9effa6/mod.ts";

Deno.test("it should support regular expressions", (done) => {
const app = opine();

app.get("/", (req, res) => {
res.send("hey");
});

superdeno(app)
.get("/")
.expect("Content-Type", /^application/)
.end((err) => {
expect(err.message).toEqual(
'expected "Content-Type" matching /^application/, got "text/html; charset=utf-8"'
);
done();
});
});
```

Here's an example of SuperDeno working with the Oak web framework:

```ts
import { Application, Router } from "https://deno.land/x/oak@v5.0.0/mod.ts";

Deno.test("it should support the Oak framework", (done) => {
const router = new Router();
router.get("/", (ctx) => {
ctx.response.body = "hello";
});

const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());

const controller = new AbortController();
const { signal } = controller;

app.addEventListener("listen", ({ hostname, port, secure }) => {
const protocol = secure ? "https" : "http";
const url = `${protocol}://${hostname}:${port}`;

superdeno(url)
.get("/")
.expect("hello", () => {
controller.abort();
done();
});
});

await app.listen({ port: 0, signal });
});
```

For further examples, see the [tests](./test) or the [supertest examples](https://github.com/visionmedia/supertest#example) for inspiration.

## Docs

- [SuperDeno Type Docs](https://asos-craigmorten.github.io/superdeno/)
- [SuperDeno Deno Docs](https://doc.deno.land/https/deno.land/x/superdeno/mod.ts)
- [License](https://github.com/asos-craigmorten/superdeno/blob/master/LICENSE.md)
- [Changelog](https://github.com/asos-craigmorten/superdeno/blob/master/.github/CHANGELOG.md)

## API

You may use any [superagent](http://github.com/visionmedia/superagent) client (browser) methods and perform assertions in the `.end()` callback for lower-level needs.

### .expect(status[, fn])

Assert response `status` code.

### .expect(status, body[, fn])

Assert response `status` code and `body`.

### .expect(body[, fn])

Assert response `body` text with a string, regular expression, or parsed body object.

### .expect(field, value[, fn])

Assert header `field` `value` with a string or regular expression.

### .expect(function(res) {})

Pass a custom assertion function. It'll be given the response object to check. If the check fails, throw an error.

```ts
superdeno(app).get("/").expect(hasPreviousAndNextKeys).end(done);

function hasPreviousAndNextKeys(res) {
if (!("next" in res.parsedBody)) throw new Error("missing next key");
if (!("prev" in res.parsedBody)) throw new Error("missing prev key");
}
```

### .end(fn)

Perform the request and invoke `fn(err, res)`.

## Notes

This is a port (not fork) of [supertest](https://github.com/visionmedia/supertest) to TypeScript + Deno, which fulfills this motivation currently for Node. This module also includes a XHR sham so [superagent](https://visionmedia.github.io/superagent/) client mode can be used directly.

## Contributing

[Contributing guide](https://github.com/asos-craigmorten/opine/blob/master/.github/CONTRIBUTING.md)
[Contributing guide](https://github.com/asos-craigmorten/superdeno/blob/master/.github/CONTRIBUTING.md)

---

## License

This library is a port of [supertest](https://github.com/visionmedia/supertest) whose license and copyrights are available at [SUPERTEST_LICENSE](./SUPERTEST_LICENSE.md) in the root of this repository, and covers all files within the [source](./src) directory which detail that the file is a port.

superdeno is licensed under the [MIT License](./LICENSE.md).
43 changes: 43 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Security Policies and Procedures

This document outlines security procedures and general policies for the superdeno
project.

- [Reporting a Bug](#reporting-a-bug)
- [Disclosure Policy](#disclosure-policy)
- [Comments on this Policy](#comments-on-this-policy)

## Reporting a Bug

The superdeno team and community take all security bugs in superdeno seriously.
Thank you for improving the security of superdeno. We appreciate your efforts and
responsible disclosure and will make every effort to acknowledge your
contributions.

Report security bugs by emailing the lead maintainer in the
[CODEOWNERS](./.github/CODEOWNERS.md) file.

The lead maintainer will acknowledge your email, and will send a response
indicating the next steps in handling your report. After the initial reply
to your report, the maintainer team will endeavor to keep you informed of the
progress towards a fix and full announcement, and may ask for additional
information or guidance.

Report security bugs in third-party modules to the person or team maintaining
the module.

## Disclosure Policy

When the maintainer team receives a security bug report, they will assign it to a
primary handler. This person will coordinate the fix and release process,
involving the following steps:

- Confirm the problem and determine the affected versions.
- Audit code to find any potential similar problems.
- Prepare fixes for all releases still under maintenance. These fixes will be
released as fast as possible to GitHub.

## Comments on this Policy

If you have suggestions on how this process could be improved please submit a
pull request.
22 changes: 22 additions & 0 deletions SUPERTEST_LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2014 TJ Holowaychuk <tj@vision-media.ca>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading

0 comments on commit ecc3da4

Please sign in to comment.