This commit is contained in:
thosakwe 2017-03-02 17:06:02 -05:00
parent 4b4149dea9
commit 013968336c
5 changed files with 58 additions and 5 deletions

View file

@ -1,6 +1,6 @@
# angel_framework
[![pub 1.0.0-dev.61](https://img.shields.io/badge/pub-1.0.0--dev.61-red.svg)](https://pub.dartlang.org/packages/angel_framework)
[![pub 1.0.0-dev.62](https://img.shields.io/badge/pub-1.0.0--dev.62-red.svg)](https://pub.dartlang.org/packages/angel_framework)
[![build status](https://travis-ci.org/angel-dart/framework.svg)](https://travis-ci.org/angel-dart/framework)
Core libraries for the Angel Framework.

View file

@ -11,7 +11,7 @@ class RequestContext extends Extensible {
BodyParseResult _body;
ContentType _contentType;
HttpRequest _io;
String _path;
String _override, _path;
/// Additional params to be passed to services.
final Map serviceParams = {};
@ -38,7 +38,12 @@ class RequestContext extends Extensible {
String get ip => remoteAddress.address;
/// This request's HTTP method.
String get method => io.method;
///
/// This may have been processed by an override. See [originalMethod] to get the real method.
String get method => _override ?? originalMethod;
/// The original HTTP verb sent to the server.
String get originalMethod => io.method;
/// All post data submitted to the server.
Map get body => _body.body;
@ -85,8 +90,16 @@ class RequestContext extends Extensible {
static Future<RequestContext> from(HttpRequest request, Angel app) async {
RequestContext ctx = new RequestContext();
String override = request.method;
if (app.allowMethodOverrides == true)
override =
request.headers.value('x-http-method-override')?.toUpperCase() ??
request.method;
ctx.app = app;
ctx._contentType = request.headers.contentType;
ctx._override = override;
ctx._path = request.uri
.toString()
.replaceAll("?" + request.uri.query, "")

View file

@ -42,6 +42,9 @@ class Angel extends AngelBase {
final Map<dynamic, InjectionRequest> _preContained = {};
/// Determines whether to allow HTTP request method overrides.
bool allowMethodOverrides = true;
/// Fired after a request is processed. Always runs.
Stream<HttpRequest> get afterProcessed => _afterProcessed.stream;
@ -223,7 +226,7 @@ class Angel extends AngelBase {
if (requestedUrl.isEmpty) requestedUrl = '/';
var resolved =
resolveAll(requestedUrl, requestedUrl, method: request.method);
resolveAll(requestedUrl, requestedUrl, method: req.method);
for (var result in resolved) req.params.addAll(result.allParams);

View file

@ -1,5 +1,5 @@
name: angel_framework
version: 1.0.0-dev.61
version: 1.0.0-dev.62
description: Core libraries for the Angel framework.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_framework

37
test/general_test.dart Normal file
View file

@ -0,0 +1,37 @@
import 'dart:convert';
import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:http/http.dart' as http;
import 'package:test/test.dart';
main() {
Angel app;
http.Client client;
HttpServer server;
String url;
setUp(() async {
app = new Angel()
..post('/foo', () => {'hello': 'world'})
..all('*', () => throw new AngelHttpException.notFound());
client = new http.Client();
server = await app.startServer();
url = "http://${server.address.host}:${server.port}";
});
tearDown(() async {
app = null;
url = null;
client.close();
client = null;
await server.close(force: true);
});
test("allow override of method", () async {
var response = await client
.get('$url/foo', headers: {'X-HTTP-Method-Override': 'POST'});
print('Response: ${response.body}');
expect(JSON.decode(response.body), equals({'hello': 'world'}));
});
}