项目作者: avoidwork

项目描述 :
Lightweight HTTP/HTTP2 compliant test framework
高级语言: JavaScript
项目地址: git://github.com/avoidwork/tiny-httptest.git
创建时间: 2017-02-16T16:39:36Z
项目社区:https://github.com/avoidwork/tiny-httptest

开源协议:BSD 3-Clause "New" or "Revised" License

下载


Tiny HTTP Test

tiny-httptest is a lightweight HTTP test framework that makes it easy to validate CORS is working, capture cookies & HTTP response headers (including etags) and reuse them for sequential tests.

Using the factory

  1. import {httptest} from "tiny-httptest";
  2. describe('HTTP Tests', () => {
  3. it("GET /somefile (Captured the etag)", function () {
  4. return httptest({url: `http://localhost:${port}/somefile`})
  5. .etags()
  6. .expectStatus(200)
  7. .end();
  8. });
  9. it("GET /somefile (Reused the ETag)", function () {
  10. return httptest({url: `http://localhost:${port}/somefile`})
  11. .etags()
  12. .expectStatus(304)
  13. .end();
  14. });
  15. it("GET /invalid-file", function () {
  16. return httptest({url: `http://localhost:${port}/invalid-file`})
  17. .expectStatus(404)
  18. .end();
  19. });
  20. });

Using the Class

  1. import {HTTPTest} from "tiny-httptest";
  2. class MyTestRunner extends HTTPTest {}

Testing

Tiny HTTP Test has 100% code coverage with its tests. Run npm run test-setup after installing modules.

  1. Implicit proofs
  2. Starting test server
  3. GET / (captures cookie, CSRF token)
  4. HEAD / (reuses cookie)
  5. POST / (reuses cookie & CSRF token)
  6. POST / (reuses cookie & CSRF token + body)
  7. POST / (reuses cookie & CSRF token + body)
  8. GET / (CORS Pre-flight)
  9. GET / (CORS)
  10. GET /invalid (CORS)
  11. GET / (Basic Auth)
  12. GET https://google.com/ (HTTPS) (94ms)
  13. GET /assets/css/style.css
  14. GET /assets/css/style.css (ETag)
  15. Stopping test server
  16. Error proofs
  17. Starting test server
  18. GET https://invalid.local.dev/ (DNS error)
  19. INVALID / (Invalid HTTP method)
  20. GET /hello (Error thrown)
  21. GET /assets/css/style.css (Invalid 404)
  22. Stopping test server
  23. 20 passing (160ms)
  24. -------------------|---------|----------|---------|---------|-----------------------------------------------------------
  25. File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
  26. -------------------|---------|----------|---------|---------|-----------------------------------------------------------
  27. All files | 100 | 81.13 | 100 | 100 |
  28. tiny-httptest.cjs | 100 | 81.13 | 100 | 100 | 16-22,111,125-130,177,193,211-227,247,262,303,318-323,371
  29. -------------------|---------|----------|---------|---------|-----------------------------------------------------------

Options

body

HTTP request body, defaults to null but can be String, Object or Array.

headers

HTTP request headers, defaults to {}.

method

HTTP request method, defaults to GET.

timeout

HTTP request timeout as milliseconds, defaults to 30000.

url

URL & port to request, defaults to http://localhost.

API

captureHeader(name)

Captures a header to be reused by another instance of TinyHTTPTest.

cookies([state = true])

Enables or disables cookie capture & reuse.

cors([hostname, success = true])

Sets request & response header expectations, defaults to request hostname if optional argument is not supplied.

If testing an error case, you must specify the second parameter as false to not expect CORS headers.

end()

Ends the request, Promise resolves with TinyHTTPTest instance or rejects with Error.

etags([state = true])

Enables or disables ETag capture & reuse.

expectBody([value = /\w+/])

Sets an expectation of the response body, default value is a RegExp which tests if there is a response body.

expectHeader(name, [value = /\w+/])

Sets an expectation of a response header, default value is a RegExp which tests if there is a header value.

expectJson()

Sets an expectation of response header content-type, supports correct and common incorrect header values.

expectStatus([value = 200])

Sets an expectation of response status code, default value is 200.

json([arg])

Sets request & response to JSON, sends arg if not undefined.

process()

Processes the response of the TinyHTTPTest instance.

reuseHeader(name)

Reuses a captured header from another instance of TinyHTTPTest.

send(arg)

Decorates arg as request body & sets request headers.

test(arg, value, err)

Validates that arg is equal to or passes value test, throws Error with err as message if invalid.

License

Copyright (c) 2024 Jason Mulligan
Licensed under the BSD-3 license.