Skip to content

Commit

Permalink
Merge pull request #131 from 8fold/fix-redirect
Browse files Browse the repository at this point in the history
fix: redirect
  • Loading branch information
joshbruce authored Nov 18, 2021
2 parents 9da8bae + 7fbe6a4 commit c200626
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 49 deletions.
2 changes: 1 addition & 1 deletion content/public/self-improvement/content.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
redirect: /design-your-life
redirect: 301 /design-your-life
---
4 changes: 2 additions & 2 deletions content/public/self-improvement/motivators/content.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
redirect: /design-your-life/motivators
---
redirect: 301 /design-your-life/motivators
---
26 changes: 0 additions & 26 deletions package.json

This file was deleted.

14 changes: 14 additions & 0 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ public function moved(string $format = ''): string|int|false
return $this->dateField('moved', $format);
}

/* @todo: Need testing to avoid regression */
public function redirect(): object|false
{
if ($this->frontMatterHasMember('redirect')) {
$redirect = strval($this->frontMatter['redirect']);
list($code, $destination) = explode(' ', $redirect, 2);
return (object) [
'code' => intval($code),
'destination' => strval($destination)
];
}
return false;
}

private function dateField(
string $key,
string $format = ''
Expand Down
11 changes: 11 additions & 0 deletions src/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ public function statusCode(): int
} elseif ($this->isNotFound()) {
return 404;

} elseif (
$redirect = File::at(
$this->localPath(),
$this->fileSystem()
)->redirect()
) {
// TODO: create HttpRedirect??
// @phpstan-ignore-next-line
$code = $redirect->code;
return ($code >= 300 and $code <= 399) ? $code : 500;

}
return 200;
}
Expand Down
7 changes: 7 additions & 0 deletions src/HttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public function headers(): array
} elseif ($this->statusCode() === 404) {
$headers['Content-Type'] = 'text/html';

} elseif ($this->statusCode() > 300 and $this->statusCode() < 399) {
if ($redirect = $this->request()->localFile()->redirect()) {
// TODO: create HttpRedirect??
// @phpstan-ignore-next-line
$headers['Location'] = $redirect->destination;

}
}
return $headers;
}
Expand Down
18 changes: 0 additions & 18 deletions tests/ContentTest.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/FileSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
count(
TestFileSystem::init()->publishedContentFinder()
)
)->toBeInt()->toBe(3);
)->toBeInt()->toBe(6);
})->group('filesystem');

it('has required folders', function() {
Expand Down
48 changes: 47 additions & 1 deletion tests/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,52 @@
404
);

expect(
HttpResponse::from(
request: HttpRequest::with(
ServerGlobals::init()->withRequestUri('/published-redirect'),
TestFileSystem::init()
)
)->statusCode()
)->toBeInt()->toBe(
301
);

expect(
HttpResponse::from(
request: HttpRequest::with(
ServerGlobals::init()->withRequestUri('/published-redirect/302'),
TestFileSystem::init()
)
)->statusCode()
)->toBeInt()->toBe(
302
);

expect(
HttpResponse::from(
request: HttpRequest::with(
ServerGlobals::init()->withRequestUri('/published-redirect/500'),
TestFileSystem::init()
)
)->statusCode()
)->toBeInt()->toBe(
500
);

$headers = HttpResponse::from(
request: HttpRequest::with(
ServerGlobals::init()->withRequestUri('/published-redirect/302'),
TestFileSystem::init()
)
)->headers();

expect(
array_key_exists('Location', $headers)
)->toBeTrue();
})->group('response', 'request');

it('can handle 405 response', function() {
expect(
HttpResponse::from(
request: HttpRequest::with(
Expand All @@ -109,7 +155,7 @@
)->toBeInt()->toBe(
405
);
})->group('response', 'request');
});

it('can handle 500 response', function() {
$serverGlobals = TestServerGlobals::init()->unsetAppEnv();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: This page should redirect
redirect: 302 /published-sub-sub
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: This page should redirect
redirect: 400 /published-sub-sub
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: This page should redirect
redirect: 301 /published-sub-sub
---

0 comments on commit c200626

Please sign in to comment.