Coder Social home page Coder Social logo

dart-http-mocks's Introduction

Mocks for dart:io HTTP requests

Build Status Coverage Status License

Mocks and special test matchers to simplify testing of HttpRequests from dart:io.

1. Usage

// file: some_test.dart
library some_lib.tests;

import 'dart:io';
import 'package:test/test.dart';
import 'package:http_mocks/http_mocks.dart';

void main() {
  test('it mocks http requests', () async {
    var request = new HttpRequestMock(Uri.parse('/hello-world'), 'GET');
    // Pass it to your HTTP server or whatever else accepting HttpRequest
    // as an input argument:
    await yourHttpServer.handle(request);

    // Use provided matchers:
    expect(request, responseStatus(HttpStatus.OK));
    expect(request, responseContentType(ContentType.TEXT));
    expect(request,
      responseHeaders(containsPair('Access-Control-Allow-Origin', '*')));
    expect(request, responseBody(contains('Hello world!')));
    expect(request, responseSent); // makes sure request has been "closed".
  });
}

2. How it works

Under the hood this library uses mockito to create all the mocks. All mocks are available through HttpRequestMock so it is possible to perform custom expectations or verifications provided by mockito library.

var request = new HttpRequestMock(Uri.parse('/foo'), 'GET');
request.headersMock; // mock for request's headers
request.responseMock; // mock for HttpResponse
request.responseHeadersMock; // mock for response's headers

3. Limitations

Current implementation only covers only a subset of all possible use cases, though hopefully the most common ones. This means that not all the expectations can be performed.

  • The HttpHeadersMock does not support "multi-value" headers at this moment (but this can be added in the future).
  • Reading of request body assumes you will use UTF8.decodeStream().

If you find a use case which is not covered, feel free to create an issue or a pull request!

License

BSD-2

dart-http-mocks's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.