Merge pull request #1616 from dimtion/fix-api-redirect

API postLink: change relative path to absolute path
This commit is contained in:
ArthurHoaro 2020-10-29 16:03:07 +01:00 committed by GitHub
commit dff039092d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 5 deletions

View file

@ -131,7 +131,7 @@ public function postLink($request, $response)
$this->bookmarkService->add($bookmark); $this->bookmarkService->add($bookmark);
$out = ApiUtils::formatLink($bookmark, index_url($this->ci['environment'])); $out = ApiUtils::formatLink($bookmark, index_url($this->ci['environment']));
$redirect = $this->ci->router->relativePathFor('getLink', ['id' => $bookmark->getId()]); $redirect = $this->ci->router->pathFor('getLink', ['id' => $bookmark->getId()]);
return $response->withAddedHeader('Location', $redirect) return $response->withAddedHeader('Location', $redirect)
->withJson($out, 201, $this->jsonStyle); ->withJson($out, 201, $this->jsonStyle);
} }

View file

@ -92,8 +92,8 @@ protected function setUp(): void
$mock = $this->createMock(Router::class); $mock = $this->createMock(Router::class);
$mock->expects($this->any()) $mock->expects($this->any())
->method('relativePathFor') ->method('pathFor')
->willReturn('api/v1/bookmarks/1'); ->willReturn('/api/v1/bookmarks/1');
// affect @property-read... seems to work // affect @property-read... seems to work
$this->controller->getCi()->router = $mock; $this->controller->getCi()->router = $mock;
@ -128,7 +128,7 @@ public function testPostLinkMinimal()
$response = $this->controller->postLink($request, new Response()); $response = $this->controller->postLink($request, new Response());
$this->assertEquals(201, $response->getStatusCode()); $this->assertEquals(201, $response->getStatusCode());
$this->assertEquals('api/v1/bookmarks/1', $response->getHeader('Location')[0]); $this->assertEquals('/api/v1/bookmarks/1', $response->getHeader('Location')[0]);
$data = json_decode((string) $response->getBody(), true); $data = json_decode((string) $response->getBody(), true);
$this->assertEquals(self::NB_FIELDS_LINK, count($data)); $this->assertEquals(self::NB_FIELDS_LINK, count($data));
$this->assertEquals(43, $data['id']); $this->assertEquals(43, $data['id']);
@ -175,7 +175,7 @@ public function testPostLinkFull()
$response = $this->controller->postLink($request, new Response()); $response = $this->controller->postLink($request, new Response());
$this->assertEquals(201, $response->getStatusCode()); $this->assertEquals(201, $response->getStatusCode());
$this->assertEquals('api/v1/bookmarks/1', $response->getHeader('Location')[0]); $this->assertEquals('/api/v1/bookmarks/1', $response->getHeader('Location')[0]);
$data = json_decode((string) $response->getBody(), true); $data = json_decode((string) $response->getBody(), true);
$this->assertEquals(self::NB_FIELDS_LINK, count($data)); $this->assertEquals(self::NB_FIELDS_LINK, count($data));
$this->assertEquals(43, $data['id']); $this->assertEquals(43, $data['id']);