Skip to content

Commit

Permalink
Add :put :patch :delete calls
Browse files Browse the repository at this point in the history
  • Loading branch information
codekeyz committed Nov 16, 2023
1 parent de9167b commit 07df632
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
46 changes: 44 additions & 2 deletions supertest/lib/supertest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ abstract interface class Tester {
Object? body,
});

Future<http.Response> put(
String path, {
Map<String, String>? headers,
Object? body,
});

Future<http.Response> patch(
String path, {
Map<String, String>? headers,
Object? body,
});

Future<http.Response> delete(
String path, {
Map<String, String>? headers,
Object? body,
});

Future<http.Response> get(
String path, {
Map<String, String>? headers,
Expand Down Expand Up @@ -41,12 +59,36 @@ class _$TesterImpl extends Tester {
Map<String, String>? headers,
}) =>
http.get(getUri(path), headers: headers);

@override
Future<http.Response> delete(
String path, {
Map<String, String>? headers,
Object? body,
}) =>
http.delete(getUri(path), headers: headers, body: body);

@override
Future<http.Response> patch(
String path, {
Map<String, String>? headers,
Object? body,
}) =>
http.patch(getUri(path), headers: headers, body: body);

@override
Future<http.Response> put(
String path, {
Map<String, String>? headers,
Object? body,
}) =>
http.put(getUri(path), headers: headers, body: body);
}

class TestAgent {
static _$TesterImpl? _instance;

static Future<Tester> create<T>(TestApp app) async {
static Future<Tester> create(TestApp app) async {
if (_instance != null) {
await _instance!._server.close();
_instance = null;
Expand Down Expand Up @@ -77,6 +119,6 @@ Uri getServerUri(HttpServer server) {
}

Future<Tester> request<T>(T app) async {
final tester = await TestAgent.create<T>((app as dynamic).handleRequest);
final tester = await TestAgent.create((app as dynamic).handleRequest);
return tester;
}
11 changes: 4 additions & 7 deletions test/http/res.json_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ void main() {
group('.json(Object)', () {
test('should not override previous Content-Types', () async {
final app = Pharaoh().get('/', (req, res) {
return res
.type(ContentType.parse('application/vnd.example+json'))
.json({"hello": "world"});
return res.type(ContentType.parse('application/vnd.example+json')).json({"hello": "world"});
});

final result = await (await request(app)).get('/');
expect(result.headers['content-type'],
'application/vnd.example+json; charset=utf-8');
final result = await (await request<Pharaoh>(app)).get('/');
expect(result.headers['content-type'], 'application/vnd.example+json; charset=utf-8');
expect(result.statusCode, 200);
expect(result.body, '{"hello":"world"}');
});
Expand All @@ -26,7 +23,7 @@ void main() {
next(res.json(null));
});

final result = await (await request(app)).get('/');
final result = await (await request<Pharaoh>(app)).get('/');
expect(result.statusCode, 200);
expect(result.body, 'null');
});
Expand Down

0 comments on commit 07df632

Please sign in to comment.