Skip to content

Commit 5a5bb23

Browse files
AyadLaouissirenancaraujoalestiago
authored
feat: adds Response Moved Permanently (#771)
Co-authored-by: Renan <[email protected]> Co-authored-by: Alejandro Santiago <[email protected]>
1 parent ed07e12 commit 5a5bb23

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

packages/dart_frog/lib/src/response.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ class Response {
6060
},
6161
);
6262

63+
/// Create a [Response] Moved Permanently.
64+
///
65+
/// This indicates that the requested resource has moved permanently to a new
66+
/// URI. [location] is that URI. It's automatically set as the Location
67+
/// header in [headers].
68+
Response.movedPermanently({
69+
required String location,
70+
String? body,
71+
Map<String, Object> headers = const <String, Object>{},
72+
Encoding? encoding,
73+
}) : this(
74+
statusCode: 301,
75+
headers: {
76+
...headers,
77+
'Location': location,
78+
},
79+
body: body,
80+
encoding: encoding,
81+
);
82+
6383
Response._(this._response);
6484

6585
shelf.Response _response;

packages/dart_frog/test/src/response_test.dart

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,66 @@ void main() {
196196
);
197197
});
198198
});
199+
200+
group('movedPermanently', () {
201+
test('has correct location url', () {
202+
final response = Response.movedPermanently(
203+
location: 'test-location',
204+
);
205+
expect(
206+
response.headers[HttpHeaders.locationHeader],
207+
equals('test-location'),
208+
);
209+
});
210+
211+
test('has correct body', () {
212+
final response = Response.movedPermanently(
213+
location: 'location',
214+
body: 'body',
215+
);
216+
expect(response.body(), completion('body'));
217+
});
218+
219+
test('has correct body (string)', () {
220+
final response = Response.movedPermanently(
221+
location: 'location',
222+
body: 'body',
223+
);
224+
expect(response.body(), completion('body'));
225+
});
226+
227+
test('has correct body (empty)', () {
228+
final response = Response.movedPermanently(
229+
location: 'location',
230+
);
231+
expect(response.body(), completion(isEmpty));
232+
});
233+
234+
test('has correct body (json)', () {
235+
final response = Response.movedPermanently(
236+
location: 'location',
237+
body: jsonEncode(
238+
<String, dynamic>{'foo': 'bar'},
239+
),
240+
);
241+
expect(
242+
response.json(),
243+
completion(<String, dynamic>{'foo': 'bar'}),
244+
);
245+
});
246+
247+
test('has correct headers', () {
248+
const headers = <String, String>{'foo': 'bar'};
249+
final response = Response.movedPermanently(
250+
location: 'location',
251+
headers: headers,
252+
);
253+
expect(response.headers['foo'], equals('bar'));
254+
expect(
255+
response.headers[HttpHeaders.locationHeader],
256+
equals('location'),
257+
);
258+
});
259+
});
199260
});
200261
}

0 commit comments

Comments
 (0)