Skip to content

Commit

Permalink
Merge branch 'master' of github.com:xp-forge/frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Oct 30, 2023
2 parents b32ba2c + bfb9102 commit a43d7ce
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
21 changes: 16 additions & 5 deletions src/main/php/web/frontend/Frontend.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,27 @@ private function view($req, $res, $delegate, $matches= []) {
public function handle($req, $res, $target= null) {
static $NOT_FOUND= [null];

// Handle HEAD requests with GET unless explicitely specified
$method= strtolower($req->method());
$path= $req->uri()->path();

// Allow overwriting HTTP method in POST request via `_method`
if ('post' === $method && $o= $req->param('_method')) {
$method= strtolower($o);
}

// Handle HEAD requests with GET unless explicitely specified
if ('head' === $method) {
$target= $this->delegates->target($method, $path) ?? $this->delegates->target('get', $path) ?? $NOT_FOUND;
$view= $this->view($req, $res, ...$target);
$view= $this->view($req, $res, ...$target
?? $this->delegates->target($method, $path)
?? $this->delegates->target('get', $path)
?? $NOT_FOUND
);
$view->stream= false;
} else {
$target= $target ?? $this->delegates->target($method, $path) ?? $NOT_FOUND;
$view= $this->view($req, $res, ...$target);
$view= $this->view($req, $res, ...$target
?? $this->delegates->target($method, $path)
?? $NOT_FOUND
);
}

$res->header('Server', 'XP/Frontend');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function patterns_sorted_by_length() {
'#get/blogs/(?<category>[^/]+)/(?<id>[0-9]+)$#',
'#get/oauth/(?<tenant>[^/]+)/select/?$#',
'#get/users/(?<id>[^/]+)/avatar$#',
'#delete/users/(?<id>[^/]+)$#',
'#get/users/(?<id>[^/]+)$#',
'#get/blogs/stats$#',
'#get/blogs/?$#',
Expand Down
32 changes: 32 additions & 0 deletions src/test/php/web/frontend/unittest/HandlingTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,38 @@ public function post() {
);
}

#[Test]
public function delete() {
$fixture= new Frontend(new Users(), new class() implements Templates {
public function write($template, $context, $out) { /* NOOP */ }
});

$res= $this->handle($fixture, 'DELETE', '/users/1000');
Assert::equals(302, $res->status());
Assert::equals('/users?deleted=1000', $res->headers()['Location']);
}

#[Test]
public function special_method_field_overwrites_post_method() {
$fixture= new Frontend(new Users(), new class() implements Templates {
public function write($template, $context, $out) { /* NOOP */ }
});

$res= $this->handle($fixture, 'POST', '/users/1000', [], '_method=DELETE');
Assert::equals(302, $res->status());
Assert::equals('/users?deleted=1000', $res->headers()['Location']);
}

#[Test]
public function special_method_field_cannot_be_used_for_get() {
$fixture= new Frontend(new Users(), new class() implements Templates {
public function write($template, $context, $out) { /* NOOP */ }
});

$res= $this->handle($fixture, 'GET', '/users/1000?_method=DELETE');
Assert::equals(404, $res->status());
}

#[Test, Expect(class: Error::class, message: '/Cannot route PATCH requests to .+/')]
public function unsupported_route() {
$fixture= new Frontend(new Users(), new class() implements Templates {
Expand Down
1 change: 1 addition & 0 deletions src/test/php/web/frontend/unittest/MethodsInTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function patterns_sorted_by_length() {
Assert::equals(
[
'#get/users/(?<id>[^/]+)/avatar$#',
'#delete/users/(?<id>[^/]+)$#',
'#get/users/(?<id>[^/]+)$#',
'#post/users$#',
'#get/users$#',
Expand Down
8 changes: 7 additions & 1 deletion src/test/php/web/frontend/unittest/actions/Users.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace web\frontend\unittest\actions;

use web\Error;
use web\frontend\{Get, Handler, Param, Post, View};
use web\frontend\{Delete, Get, Handler, Param, Post, View};

#[Handler]
class Users {
Expand Down Expand Up @@ -48,6 +48,12 @@ public function create(
return ['created' => $id];
}

#[Delete('/users/{id}')]
public function delete($id) {
unset($this->list[$id]);
return View::redirect('/users?deleted='.urlencode($id));
}

#[Get('/users/{id}/avatar')]
public function avatar($id) {
if (!isset($this->list[$id])) {
Expand Down

0 comments on commit a43d7ce

Please sign in to comment.