Skip to content

Commit 0965d86

Browse files
authored
Add Response.unauthorized (401) constructor (#262)
1 parent 6782780 commit 0965d86

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

pkgs/shelf/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.4.0-dev
2+
3+
* Add Response.unauthorized() constructor
4+
15
## 1.3.2
26

37
* `shelf_io.dart`

pkgs/shelf/lib/src/response.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,25 @@ class Response extends Message {
180180
encoding: encoding,
181181
);
182182

183+
/// Constructs a 401 Unauthorized response.
184+
///
185+
/// This indicates indicates that the client request has not been completed
186+
/// because it lacks valid authentication credentials.
187+
///
188+
/// {@macro shelf_response_body_and_encoding_param}
189+
Response.unauthorized(
190+
Object? body, {
191+
Map<String, /* String | List<String> */ Object>? headers,
192+
Encoding? encoding,
193+
Map<String, Object>? context,
194+
}) : this(
195+
401,
196+
headers: body == null ? _adjustErrorHeaders(headers) : headers,
197+
body: body ?? 'Unauthorized',
198+
context: context,
199+
encoding: encoding,
200+
);
201+
183202
/// Constructs a 403 Forbidden response.
184203
///
185204
/// This indicates that the server is refusing to fulfill the request.

pkgs/shelf/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: shelf
2-
version: 1.3.2
2+
version: 1.4.0-dev
33
description: >
44
A model for web server middleware that encourages composition and easy reuse.
55
repository: https://github.com/dart-lang/shelf/tree/master/pkgs/shelf

pkgs/shelf/test/response_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ void main() {
106106
});
107107
});
108108

109+
group('Response.unauthorized:', () {
110+
test('sets body', () {
111+
var response = Response.unauthorized('request unauthorized');
112+
expect(
113+
response.readAsString(), completion(equals('request unauthorized')));
114+
expect(response.statusCode, 401);
115+
});
116+
});
117+
109118
group('Response redirect', () {
110119
test('sets the location header for a String', () {
111120
var response = Response.found('/foo');

0 commit comments

Comments
 (0)