Browse Source

Remove the resource package

The package is no more maintained.
To use the included certificates inherited classes can be used.
master
Elnur Hajiyev 1 year ago
parent
commit
02388dc98b
  1. 48
      README.md
  2. 40
      example/example.dart
  3. 32
      lib/mock_web_server.dart
  4. 1
      pubspec.yaml
  5. 40
      test/server_test.dart

48
README.md

@ -1,4 +1,5 @@
# mock_web_server
A flexible Dart web server that can be used to script tests and web server interactions
## Summary
@ -10,20 +11,23 @@ web server.
MockWebServer aims to facility testing by offering a flexible stand alone
Server that will respond with a given script (or forward the request to your dispatcher).
MockWebServer is based on the
[library](https://github.com/square/okhttp/tree/master/mockwebserver)
MockWebServer is based on the
[library](https://github.com/square/okhttp/tree/master/mockwebserver)
of the same name created by Square for Java.
## Usage
### Starting it
MockWebServer can run in a given port or an ephemeral one
```dart
new MockWebServer(); // Will use an ephemeral port picked by the system
new MockWebServer(port: 8081); // Will use 8081
```
To start it just do
```dart
MockWebServer server = new MockWebServer();
server.start();
@ -39,7 +43,8 @@ server.host; // 127.0.0.1
```
### Adding responses to the queue
Once the server is started, you can queue the responses that you want. **The response queue is
Once the server is started, you can queue the responses that you want. **The response queue is
First In First Out**
```dart
@ -71,6 +76,7 @@ server.enqueueResponse(mockResponse);
```
### Delaying the response
To test timeouts or race conditions you may want to have the server take some time
```dart
@ -86,6 +92,7 @@ expect(response.statusCode, 201);
```
### Validating that the request was correct
You may want to check that your app is sending the correct requests. To do so you can obtain the
requests that have been made to the server. **The request queue is Last In First Out**
@ -108,7 +115,7 @@ expect(server.takeRequest().uri.path, "/third");
### Using a dispatcher for fine-grained routing
If you want more control than what the FIFO queue offers, you can set a dispatcher and set
If you want more control than what the FIFO queue offers, you can set a dispatcher and set
the logic there.
```dart
@ -138,24 +145,19 @@ expect(response.statusCode, 201);
```
### TLS
You can start the server using TLS by passing the `certificate` parameter
when creating the instance of MockWebServer. For example using the included certificates and the
`resource` library you would do
```dart
var chainRes =
new Resource('package:mock_web_server/certificates/server_chain.pem');
List<int> chain = await chainRes.readAsBytes();
var keyRes =
new Resource('package:mock_web_server/certificates/server_key.pem');
List<int> key = await keyRes.readAsBytes();
You can start the server using TLS by passing the `certificate` parameter
when creating the instance of MockWebServer. For example using the included certificates you would do
Certificate certificate = new Certificate()
```dart
Certificate certificate = Certificate.make(serverChain: 'your_server_chain.pem', serverKey: 'your_server_key.pem')
..password = "dartdart"
..key = key
..chain = chain;
// To use the included certificates:
// Certificate certificate = Certificate.included();
MockWebServer _server =
new MockWebServer(certificate: certificate);
```
@ -165,12 +167,15 @@ proper `SecurityContext`, for example using the included `trusted_certs.pem`
```dart
var certRes =
new Resource('package:mock_web_server/certificates/trusted_certs.pem');
new File('your_trusted_certs.pem');
List<int> cert = await certRes.readAsBytes();
SecurityContext clientContext = new SecurityContext()
..setTrustedCertificatesBytes(cert);
// To use the included trusted_certs.pem:
// SecurityContext clientContext = DefaultSecurityContext();
var client = new HttpClient(context: clientContext);
```
@ -178,8 +183,9 @@ var client = new HttpClient(context: clientContext);
Please check the tests of MockWebServer to see a complete example of this.
### IPv6
If want to use IPv6, you can pass `addressType: InternetAddressType.IP_V6` in the
constructor to have the server use it. Keep in mind that the `host` property
If want to use IPv6, you can pass `addressType: InternetAddressType.IP_V6` in the
constructor to have the server use it. Keep in mind that the `host` property
will then be `::1` instead of `127.0.0.1`.
```dart
@ -189,6 +195,7 @@ MockWebServer _server =
```
### Setting a default response
In some scenarions, if there's nothing on queue and there's no dispatcher, you
may want the server to default to a response (e.g `404`) instead of throwing an
exception.
@ -198,4 +205,5 @@ _server.defaultResponse = MockResponse()..httpCode = 404;
```
### Stopping
During the `tearDown` of your tests you should stop the server. `server.shutdown()` will do.

40
example/example.dart

@ -19,7 +19,6 @@ import 'package:test/test.dart';
import 'dart:io';
import 'dart:async';
import 'dart:convert';
import 'package:resource/resource.dart' show Resource;
MockWebServer _server;
@ -241,21 +240,8 @@ void main() {
});
test("TLS info", () async {
var chainRes =
new Resource('package:mock_web_server/certificates/server_chain.pem');
List<int> chain = await chainRes.readAsBytes();
var keyRes =
new Resource('package:mock_web_server/certificates/server_key.pem');
List<int> key = await keyRes.readAsBytes();
Certificate certificate = new Certificate()
..password = "dartdart"
..key = key
..chain = chain;
MockWebServer _server =
new MockWebServer(port: 8029, certificate: certificate);
new MockWebServer(port: 8029, certificate: Certificate.included());
await _server.start();
RegExp url = new RegExp(r'(?:https:\/\/(?:127\.0\.0\.1):8029\/)');
@ -271,28 +257,11 @@ void main() {
test("TLS cert", () async {
String body = "S03E08 You Are Not Safe";
var chainRes =
new Resource('package:mock_web_server/certificates/server_chain.pem');
List<int> chain = await chainRes.readAsBytes();
var keyRes =
new Resource('package:mock_web_server/certificates/server_key.pem');
List<int> key = await keyRes.readAsBytes();
Certificate certificate = new Certificate()
..password = "dartdart"
..key = key
..chain = chain;
MockWebServer _server =
new MockWebServer(port: 8029, certificate: certificate);
new MockWebServer(port: 8029, certificate: Certificate.included());
await _server.start();
_server.enqueue(body: body);
var certRes =
new Resource('package:mock_web_server/certificates/trusted_certs.pem');
List<int> cert = await certRes.readAsBytes();
// Calling without the proper security context
var clientErr = new HttpClient();
@ -300,10 +269,7 @@ void main() {
throwsA(new TypeMatcher<HandshakeException>()));
// Testing with security context
SecurityContext clientContext = new SecurityContext()
..setTrustedCertificatesBytes(cert);
var client = new HttpClient(context: clientContext);
var client = new HttpClient(context: DefaultSecurityContext());
var request = await client.getUrl(Uri.parse(_server.url));
String response = await _read(await request.close());

32
lib/mock_web_server.dart

@ -68,6 +68,38 @@ class Certificate {
List<int> chain;
List<int> key;
String password;
Certificate({this.chain, this.key, this.password = "dartdart"});
factory Certificate.make({serverChain, serverKey}) {
var chainRes = new File(serverChain);
List<int> chain = chainRes.readAsBytesSync();
var keyRes = new File(serverKey);
List<int> key = keyRes.readAsBytesSync();
Certificate certificate = new Certificate(key: key, chain: chain);
return certificate;
}
factory Certificate.included() {
var chainRes = new File('lib/certificates/server_chain.pem');
List<int> chain = chainRes.readAsBytesSync();
var keyRes = new File('lib/certificates/server_key.pem');
List<int> key = keyRes.readAsBytesSync();
Certificate certificate = new Certificate(key: key, chain: chain);
return certificate;
}
}
abstract class DefaultSecurityContext extends SecurityContext {
factory DefaultSecurityContext() {
var certRes = new File('lib/certificates/trusted_certs.pem');
List<int> cert = certRes.readAsBytesSync();
return new SecurityContext()..setTrustedCertificatesBytes(cert);
}
}
/// A Web Server that can be scripted. Useful for Integration Tests, for demos,

1
pubspec.yaml

@ -6,7 +6,6 @@ homepage: https://git.starcarr.co/Dart/MockWebServer
dev_dependencies:
test: '>=1.5.1 <2.0.0'
resource: '>=2.1.5 <3.0.0'
environment:
sdk: '>=2.0.0 <3.0.0'

40
test/server_test.dart

@ -19,7 +19,6 @@ import 'package:test/test.dart';
import 'dart:io';
import 'dart:async';
import 'dart:convert';
import 'package:resource/resource.dart' show Resource;
MockWebServer _server;
@ -241,21 +240,8 @@ void main() {
});
test("TLS info", () async {
var chainRes =
new Resource('package:mock_web_server/certificates/server_chain.pem');
List<int> chain = await chainRes.readAsBytes();
var keyRes =
new Resource('package:mock_web_server/certificates/server_key.pem');
List<int> key = await keyRes.readAsBytes();
Certificate certificate = new Certificate()
..password = "dartdart"
..key = key
..chain = chain;
MockWebServer _server =
new MockWebServer(port: 8029, certificate: certificate);
new MockWebServer(port: 8029, certificate: Certificate.included());
await _server.start();
RegExp url = new RegExp(r'(?:https:\/\/(?:127\.0\.0\.1):8029\/)');
@ -271,28 +257,11 @@ void main() {
test("TLS cert", () async {
String body = "S03E08 You Are Not Safe";
var chainRes =
new Resource('package:mock_web_server/certificates/server_chain.pem');
List<int> chain = await chainRes.readAsBytes();
var keyRes =
new Resource('package:mock_web_server/certificates/server_key.pem');
List<int> key = await keyRes.readAsBytes();
Certificate certificate = new Certificate()
..password = "dartdart"
..key = key
..chain = chain;
MockWebServer _server =
new MockWebServer(port: 8029, certificate: certificate);
new MockWebServer(port: 8029, certificate: Certificate.included());
await _server.start();
_server.enqueue(body: body);
var certRes =
new Resource('package:mock_web_server/certificates/trusted_certs.pem');
List<int> cert = await certRes.readAsBytes();
// Calling without the proper security context
var clientErr = new HttpClient();
@ -300,10 +269,7 @@ void main() {
throwsA(new TypeMatcher<HandshakeException>()));
// Testing with security context
SecurityContext clientContext = new SecurityContext()
..setTrustedCertificatesBytes(cert);
var client = new HttpClient(context: clientContext);
var client = new HttpClient(context: DefaultSecurityContext());
var request = await client.getUrl(Uri.parse(_server.url));
String response = await _read(await request.close());

Loading…
Cancel
Save