Fix a broken test for redirects (#317)

This test added a `.catchError` callback with some expects, but the
`Future` was completing normally so the callback and expects never ran.
The reason the response came back successfully was that the IO client
does not consider a `302` to be a redirect when the method is `'POST'`.

https://github.com/dart-lang/sdk/blob/ed9e89ea388e4bc0142bf6e967d4ca11999bdfdc/sdk/lib/_http/http_impl.dart#L355-L365

- Refactor to async/await to clean up the noise of `.then`,
  `.whenComplete`, and `.catchError` and make the logic easier to
  follow.
- Switch the request type to `'GET'` so that the exception occurs.
- Move server starting and stopping to a `setUp` and `tearDown`.
- Fix the expectation to look for the wrapped `ClientException` instead
  of the exception thrown by the IO client. We also lose the underlying
  exception and have only the message so we can't check the length of
  the `redirects` field.
- Remove the now unused test utility to get a matcher on the old
  exception type.
2 files changed
tree: 8060edf32b4a7a058bed573e2d34c20dfef55a10
  1. .github/
  2. example/
  3. lib/
  4. test/
  5. .gitignore
  6. .test_config
  7. .travis.yml
  8. analysis_options.yaml
  9. CHANGELOG.md
  10. LICENSE
  11. pubspec.yaml
  12. README.md
README.md

A composable, Future-based library for making HTTP requests.

pub package Build Status

This package contains a set of high-level functions and classes that make it easy to consume HTTP resources. It's platform-independent, and can be used on both the command-line and the browser.

Using

The easiest way to use this library is via the top-level functions. They allow you to make individual HTTP requests with minimal hassle:

import 'package:http/http.dart' as http;

var url = 'https://example.com/whatsit/create';
var response = await http.post(url, body: {'name': 'doodle', 'color': 'blue'});
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');

print(await http.read('https://example.com/foobar.txt'));

If you‘re making multiple requests to the same server, you can keep open a persistent connection by using a Client rather than making one-off requests. If you do this, make sure to close the client when you’re done:

var client = http.Client();
try {
  var uriResponse = await client.post('https://example.com/whatsit/create',
      body: {'name': 'doodle', 'color': 'blue'});
  print(await client.get(uriResponse.bodyFields['uri']));
} finally {
  client.close();
}

You can also exert more fine-grained control over your requests and responses by creating Request or StreamedRequest objects yourself and passing them to Client.send.

This package is designed to be composable. This makes it easy for external libraries to work with one another to add behavior to it. Libraries wishing to add behavior should create a subclass of BaseClient that wraps another Client and adds the desired behavior:

class UserAgentClient extends http.BaseClient {
  final String userAgent;
  final http.Client _inner;

  UserAgentClient(this.userAgent, this._inner);

  Future<StreamedResponse> send(BaseRequest request) {
    request.headers['user-agent'] = userAgent;
    return _inner.send(request);
  }
}