-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
103 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
/// Common HTTP utilities for JSON:API clients and servers. | ||
/// WARNING: This library is in beta stage. The API is subject to change. | ||
library http; | ||
export 'package:json_api/src/http/status_code.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
class NotAcceptable implements Exception {} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import 'package:http_interop/http_interop.dart'; | ||
import 'package:json_api/http.dart'; | ||
import 'package:json_api/server.dart'; | ||
import 'package:test/expect.dart'; | ||
import 'package:test/scaffolding.dart'; | ||
|
||
void main() { | ||
final get = Request('get', Uri(), Body(), Headers()); | ||
|
||
group('default handlers', () { | ||
final converter = errorConverter(); | ||
test('can catch MethodNotAllowed', () async { | ||
final r = await converter((_) => throw MethodNotAllowed('foo'))(get); | ||
expect(r.statusCode, equals(StatusCode.methodNotAllowed)); | ||
}); | ||
test('can catch UnmatchedTarget', () async { | ||
final r = await converter((_) => throw UnmatchedTarget(Uri()))(get); | ||
expect(r.statusCode, equals(StatusCode.badRequest)); | ||
}); | ||
test('can catch CollectionNotFound', () async { | ||
final r = await converter((_) => throw CollectionNotFound('foo'))(get); | ||
expect(r.statusCode, equals(StatusCode.notFound)); | ||
}); | ||
test('can catch ResourceNotFound', () async { | ||
final r = | ||
await converter((_) => throw ResourceNotFound('foo', 'bar'))(get); | ||
expect(r.statusCode, equals(StatusCode.notFound)); | ||
}); | ||
test('can catch RelationshipNotFound', () async { | ||
final r = await converter( | ||
(_) => throw RelationshipNotFound('foo', 'bar', 'baz'))(get); | ||
expect(r.statusCode, equals(StatusCode.notFound)); | ||
}); | ||
test('can catch UnsupportedMediaType', () async { | ||
final r = await converter((_) => throw UnsupportedMediaType())(get); | ||
expect(r.statusCode, equals(StatusCode.unsupportedMediaType)); | ||
}); | ||
test('can catch Unacceptable', () async { | ||
final r = await converter((_) => throw NotAcceptable())(get); | ||
expect(r.statusCode, equals(StatusCode.notAcceptable)); | ||
}); | ||
test('can catch any other error', () async { | ||
final r = await converter((_) => throw 'foo')(get); | ||
expect(r.statusCode, equals(StatusCode.internalServerError)); | ||
}); | ||
}); | ||
|
||
group('custom handlers', () { | ||
final converter = errorConverter( | ||
onMethodNotAllowed: (_) async => Response(550, Body(), Headers()), | ||
onUnmatchedTarget: (_) async => Response(551, Body(), Headers()), | ||
onCollectionNotFound: (_) async => Response(552, Body(), Headers()), | ||
onResourceNotFound: (_) async => Response(553, Body(), Headers()), | ||
onRelationshipNotFound: (_) async => Response(554, Body(), Headers()), | ||
onError: (_, __) async => Response(555, Body(), Headers()), | ||
); | ||
test('can catch MethodNotAllowed', () async { | ||
final r = await converter((_) => throw MethodNotAllowed('foo'))(get); | ||
expect(r.statusCode, equals(550)); | ||
}); | ||
test('can catch UnmatchedTarget', () async { | ||
final r = await converter((_) => throw UnmatchedTarget(Uri()))(get); | ||
expect(r.statusCode, equals(551)); | ||
}); | ||
test('can catch CollectionNotFound', () async { | ||
final r = await converter((_) => throw CollectionNotFound('foo'))(get); | ||
expect(r.statusCode, equals(552)); | ||
}); | ||
test('can catch ResourceNotFound', () async { | ||
final r = | ||
await converter((_) => throw ResourceNotFound('foo', 'bar'))(get); | ||
expect(r.statusCode, equals(553)); | ||
}); | ||
test('can catch RelationshipNotFound', () async { | ||
final r = await converter( | ||
(_) => throw RelationshipNotFound('foo', 'bar', 'baz'))(get); | ||
expect(r.statusCode, equals(554)); | ||
}); | ||
test('can catch any other error', () async { | ||
final r = await converter((_) => throw 'foo')(get); | ||
expect(r.statusCode, equals(555)); | ||
}); | ||
}); | ||
} |