angel_test 2.0.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 74

angel_test #

Pub build status

Testing utility library for the Angel framework.

TestClient #

The TestClient class is a custom angel_client that sends mock requests to your server. This means that you will not have to bind your server to HTTP to run. Plus, it is an angel_client, and thus supports services and other goodies.

The TestClient also supports WebSockets. WebSockets cannot be mocked (yet!) within this library, so calling the websocket() function will also bind your server to HTTP, if it is not already listening.

The return value is a WebSockets client instance (from package:angel_websocket);

var ws = await client.websocket('/ws');
ws.service('api/users').onCreated.listen(...);

// To receive all blobs of data sent on the WebSocket:
ws.onData.listen(...);

Matchers #

Several Matchers are bundled with this package, and run on any package:http Response, not just those returned by Angel.

test('foo', () async {
    var res = await client.get('/foo');
    expect(res, allOf([
        isJson({'foo': 'bar'}),
        hasStatus(200),
        hasContentType(ContentType.JSON),
        hasContentType('application/json'),
        hasHeader('server'), // Assert header present
        hasHeader('server', 'angel'), // Assert header present with value
        hasHeader('foo', ['bar', 'baz']), // ... Or multiple values
        hasBody(), // Assert non-empty body
        hasBody('{"foo":"bar"}') // Assert specific body
    ]));
});

test('error', () async {
    var res = await client.get('/error');
    expect(res, isAngelHttpException());
    expect(res, isAngelHttpException(statusCode: 404, message: ..., errors: [...])) // Optional
});

hasValidBody is one of the most powerful Matchers in this library, because it allows you to validate a JSON body against a validation schema.

Angel provides a comprehensive validation library that integrates tightly with the very matcher package that you already use for testing. :)

https://github.com/angel-dart/validate

test('validate response', () async {
    var res = await client.get('/bar');
    expect(res, hasValidBody(new Validator({
        'foo': isBoolean,
        'bar': [isString, equals('baz')],
        'age*': [],
        'nested': someNestedValidator
    })));
});

2.0.1 #

  • Update badge.
  • Handle userInfo + basic auth.

2.0.0 #

  • Update to work with client@2.

2.0.0-alpha.4 #

2.0.0-alpha.3 #

  • Update http dependency.

2.0.0-alpha.2 #

  • Explicitly import package:angel_framework/http.dart.

2.0.0-alpha.1 #

  • Update for compliance with newer angel_client.

2.0.0-alpha #

  • Depend on Dart 2 and Angel 2.

1.1.0+1 #

  • Dart 2/strong mode fixes.
  • Pass a useZone flag to AngelHttp through TestServer.

example/main.dart

import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_test/angel_test.dart';
import 'package:angel_validate/angel_validate.dart';
import 'package:angel_websocket/server.dart';
import 'package:test/test.dart';

main() {
  Angel app;
  TestClient client;

  setUp(() async {
    app = new Angel()
      ..get('/hello', (req, res) => 'Hello')
      ..get(
          '/error',
          (req, res) => throw new AngelHttpException.forbidden(message: 'Test')
            ..errors.addAll(['foo', 'bar']))
      ..get('/body', (req, res) {
        res
          ..write('OK')
          ..close();
      })
      ..get(
          '/valid',
          (req, res) => {
                'michael': 'jackson',
                'billie': {'jean': 'hee-hee', 'is_my_lover': false}
              })
      ..post('/hello', (req, res) async {
        var body = await req.parseBody().then((_) => req.bodyAsMap);
        return {'bar': body['foo']};
      })
      ..get('/gzip', (req, res) async {
        res
          ..headers['content-encoding'] = 'gzip'
          ..add(gzip.encode('Poop'.codeUnits))
          ..close();
      })
      ..use(
          '/foo',
          new AnonymousService(
              index: ([params]) async => [
                    {'michael': 'jackson'}
                  ],
              create: (data, [params]) async => {'foo': 'bar'}));

    var ws = new AngelWebSocket(app);
    await app.configure(ws.configureServer);
    app.all('/ws', ws.handleRequest);

    app.errorHandler = (e, req, res) => e.toJson();

    client = await connectTo(app);
  });

  tearDown(() async {
    await client.close();
    app = null;
  });

  group('matchers', () {
    group('isJson+hasStatus', () {
      test('get', () async {
        final response = await client.get('/hello');
        expect(response, isJson('Hello'));
      });

      test('post', () async {
        final response = await client.post('/hello', body: {'foo': 'baz'});
        expect(response, allOf(hasStatus(200), isJson({'bar': 'baz'})));
      });
    });

    test('isAngelHttpException', () async {
      var res = await client.get('/error');
      print(res.body);
      expect(res, isAngelHttpException());
      expect(
          res,
          isAngelHttpException(
              statusCode: 403, message: 'Test', errors: ['foo', 'bar']));
    });

    test('hasBody', () async {
      var res = await client.get('/body');
      expect(res, hasBody());
      expect(res, hasBody('OK'));
    });

    test('hasHeader', () async {
      var res = await client.get('/hello');
      expect(res, hasHeader('server'));
      expect(res, hasHeader('server', 'angel'));
      expect(res, hasHeader('server', ['angel']));
    });

    test('hasValidBody+hasContentType', () async {
      var res = await client.get('/valid');
      expect(res, hasContentType('application/json'));
      expect(
          res,
          hasValidBody(new Validator({
            'michael*': [isString, isNotEmpty, equals('jackson')],
            'billie': new Validator({
              'jean': [isString, isNotEmpty],
              'is_my_lover': [isBool, isFalse]
            })
          })));
    });

    test('gzip decode', () async {
      var res = await client.get('/gzip');
      expect(res, hasHeader('content-encoding', 'gzip'));
      expect(res, hasBody('Poop'));
    });

    group('service', () {
      test('index', () async {
        var foo = client.service('foo');
        var result = await foo.index();
        expect(result, [
          {'michael': 'jackson'}
        ]);
      });

      test('index', () async {
        var foo = client.service('foo');
        var result = await foo.create({});
        expect(result, {'foo': 'bar'});
      });
    });

    test('websocket', () async {
      var ws = await client.websocket();
      var foo = ws.service('foo');
      foo.create({});
      var result = await foo.onCreated.first;
      expect(result.data, equals({'foo': 'bar'}));
    });
  });
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  angel_test: ^2.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:angel_test/angel_test.dart';
  
Version Uploaded Documentation Archive
2.0.1 Jan 6, 2019 Go to the documentation of angel_test 2.0.1 Download angel_test 2.0.1 archive
2.0.0 Jan 6, 2019 Go to the documentation of angel_test 2.0.0 Download angel_test 2.0.0 archive
1.1.0+1 Jul 9, 2018 Go to the documentation of angel_test 1.1.0+1 Download angel_test 1.1.0+1 archive
1.1.0 Dec 7, 2017 Go to the documentation of angel_test 1.1.0 Download angel_test 1.1.0 archive
1.0.6 Jun 3, 2017 Go to the documentation of angel_test 1.0.6 Download angel_test 1.0.6 archive
1.0.5+1 May 27, 2017 Go to the documentation of angel_test 1.0.5+1 Download angel_test 1.0.5+1 archive
1.0.5 Apr 25, 2017 Go to the documentation of angel_test 1.0.5 Download angel_test 1.0.5 archive
1.0.4 Mar 29, 2017 Go to the documentation of angel_test 1.0.4 Download angel_test 1.0.4 archive
1.0.3 Mar 29, 2017 Go to the documentation of angel_test 1.0.3 Download angel_test 1.0.3 archive
1.0.2 Mar 25, 2017 Go to the documentation of angel_test 1.0.2 Download angel_test 1.0.2 archive

All 21 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
54
Health:
Code health derived from static analysis. [more]
89
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
74
Learn more about scoring.

We analyzed this package on Jun 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.2
  • pana: 0.12.18

Platforms

Detected platforms: other

Primary library: package:angel_test/angel_test.dart with components: io, mirrors.

Health suggestions

Fix lib/src/matchers.dart. (-6.78 points)

Analysis of lib/src/matchers.dart reported 14 hints, including:

line 12 col 65: Use = to separate a named parameter from its default value.

line 73 col 9: DO use curly braces for all flow control structures.

line 75 col 9: DO use curly braces for all flow control structures.

line 123 col 7: DO use curly braces for all flow control structures.

line 125 col 7: DO use curly braces for all flow control structures.

Fix lib/src/client.dart. (-4.89 points)

Analysis of lib/src/client.dart reported 10 hints, including:

line 28 col 24: Use = to separate a named parameter from its default value.

line 29 col 17: Use = to separate a named parameter from its default value.

line 30 col 12: 'isProduction' is deprecated and shouldn't be used.

line 32 col 40: DO use curly braces for all flow control structures.

line 59 col 47: Use = to separate a named parameter from its default value.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
angel_client ^2.0.0 2.0.2
angel_framework ^2.0.0-alpha 2.0.3
angel_http_exception ^1.0.0 1.1.0
angel_validate ^2.0.0 2.0.1
angel_websocket ^2.0.0 2.0.3
http ^0.12.0 0.12.0+2
matcher ^0.12.0 0.12.5
mock_request ^1.0.0 1.0.5
web_socket_channel ^1.0.0 1.0.13
Transitive dependencies
angel_auth 2.1.5+1
angel_container 1.0.4
angel_model 1.0.3
angel_route 3.0.6
async 2.2.0
charcode 1.1.2
code_buffer 1.0.1
collection 1.14.11
combinator 1.1.0
convert 2.1.1
crypto 2.0.6
dart2_constant 1.0.2+dart2
file 5.0.8
http2 1.0.0
http_parser 3.1.3
http_server 0.9.8+1
intl 0.15.8
json_god 2.0.0-beta+3
logging 0.11.3+2
merge_map 1.0.2
meta 1.1.7
mime 0.9.6+3
path 1.6.2
pedantic 1.7.0
quiver 2.0.3
quiver_hashcode 2.0.0
source_span 1.5.5
stack_trace 1.9.3
stream_channel 2.0.0
string_scanner 1.0.4
term_glyph 1.1.0
tuple 1.0.2
typed_data 1.1.6
uuid 2.0.2
Dev dependencies
test ^1.0.0