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

Add tests for Message #90

Merged
merged 4 commits into from
Jun 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export 'src/base_client.dart';
export 'src/client.dart';
export 'src/exception.dart';
export 'src/io_client.dart';
export 'src/multipart_file.dart';
export 'src/multipart_request.dart';
export 'src/middleware.dart';
export 'src/request.dart';
export 'src/response.dart';

Expand Down
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ dependencies:
path: ">=0.9.0 <2.0.0"
stack_trace: ">=0.9.1 <2.0.0"
dev_dependencies:
unittest: ">=0.9.0 <0.12.0"
test: "^0.12.18"
# Override dependency on package_resolver to enable test
dependency_overrides:
package_resolver: '^1.0.0'
Copy link
Member

Choose a reason for hiding this comment

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

It might be nice to add some newlines to make the pubspec a bit more readable.

Copy link
Author

Choose a reason for hiding this comment

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

At this point I was figuring we just fix it up when releasing 0.12.0 into the wild

Copy link
Member

Choose a reason for hiding this comment

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

Sure.

environment:
sdk: ">=1.24.0-dev.0.0 <2.0.0"
100 changes: 100 additions & 0 deletions test/message_change_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:convert';

import 'package:test/test.dart';

import 'package:http/http.dart';
import 'package:http/src/message.dart';

final _uri = Uri.parse('http://localhost/');

void main() {
group('Request', () {
_testChange(({body, headers, context}) {
return new Request('GET', _uri,
body: body, headers: headers, context: context);
});
});

group('Response', () {
_testChange(({body, headers, context}) {
return new Response(_uri, 200,
body: body, headers: headers, context: context);
});
});
}

/// Shared test method used by [Request] and [Response] tests to validate
/// the behavior of `change` with different `headers` and `context` values.
void _testChange(
Message factory(
{body, Map<String, String> headers, Map<String, Object> context})) {
group('body', () {
test('with String', () async {
var request = factory(body: 'Hello, world');
var copy = request.change(body: 'Goodbye, world');

var newBody = await copy.readAsString();

expect(newBody, equals('Goodbye, world'));
});

test('with Stream', () async {
var request = factory(body: 'Hello, world');
var copy = request.change(
body: new Stream.fromIterable(['Goodbye, world'])
.transform(UTF8.encoder));

var newBody = await copy.readAsString();

expect(newBody, equals('Goodbye, world'));
});
});

test('with empty headers returns identical instance', () {
var request = factory(headers: {'header1': 'header value 1'});
var copy = request.change(headers: {});

expect(copy.headers, same(request.headers));
});

test('with empty context returns identical instance', () {
var request = factory(context: {'context1': 'context value 1'});
var copy = request.change(context: {});

expect(copy.context, same(request.context));
});

test('new header values are added', () {
var request = factory(headers: {'test': 'test value'});
var copy = request.change(headers: {'test2': 'test2 value'});

expect(copy.headers,
{'test': 'test value', 'test2': 'test2 value', 'content-length': '0'});
});

test('existing header values are overwritten', () {
var request = factory(headers: {'test': 'test value'});
var copy = request.change(headers: {'test': 'new test value'});

expect(copy.headers, {'test': 'new test value', 'content-length': '0'});
});

test('new context values are added', () {
var request = factory(context: {'test': 'test value'});
var copy = request.change(context: {'test2': 'test2 value'});

expect(copy.context, {'test': 'test value', 'test2': 'test2 value'});
});

test('existing context values are overwritten', () {
var request = factory(context: {'test': 'test value'});
var copy = request.change(context: {'test': 'new test value'});

expect(copy.context, {'test': 'new test value'});
});
}
Loading