8000 [Bugfix] Correct URLs in encoded output when no URL namespace · martynling/laravel-json-api@58a4949 · GitHub
[go: up one dir, main page]

Skip to content

Commit 58a4949

Browse files
committed
[Bugfix] Correct URLs in encoded output when no URL namespace
Fixes cloudcreativity#142
1 parent 2d6167f commit 58a4949

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
All notable changes to this project will be documented in this file. This project adheres to
33
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
44

5+
## [0.11.5] - 2018-02-06
6+
7+
### Fixed
8+
- [#142](https://github.com/cloudcreativity/laravel-json-api/issues/142)
9+
URLs were incorrectly formed when there is no URL namespace.
10+
511
## [0.11.4] - 2018-01-25
612

713
### Fixed

src/Api/Url.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Url
5151
public function __construct($host, $namespace, $name)
5252
{
5353
$this->host = rtrim($host, '/');
54-
$this->namespace = '/' . ltrim($namespace, '/');
54+
$this->namespace = $namespace ? '/' . ltrim($namespace, '/') : null;
5555
$this->name = $name;
5656
}
5757

tests/Integration/EncodingTest.php

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ public function testRequestedResourceHasRequestHost()
1919
$json = $this
2020
->withApiRoutes()
2121
->getJsonApi("http://www.example.com/api/v1/posts/$id")
22+
->assertStatus(200)
2223
->json();
2324

24-
$this->assertLinks('http://www.example.com', $id, $json);
25+
$this->assertSelfLink("http://www.example.com/api/v1/posts/$id", $json);
2526
}
2627

2728
/**
@@ -35,9 +36,62 @@ public function testRequestedResourceDoesNotHaveHost()
3536
$json = $this
3637
->withApiRoutes()
3738
->getJsonApi("http://www.example.com/api/v1/posts/$id")
39+
->assertStatus(200)
3840
->json();
3941

40-
$this->assertLinks('', $id, $json);
42+
$this->assertSelfLink("/api/v1/posts/$id", $json);
43+
}
44+
45+
/**
46+
* If there is no URL namespace, the URL must be properly formed.
47+
*/
48+
public function testRequestResourceDoesNotHaveUrlNamespace()
49+
{
50+
$id = factory(Post::class)->create()->getKey();
51+
config()->set('json-api-default.url.namespace', null);
52+
53+
$json = $this
54+
->withApiRoutes()
55+
->getJsonApi("http://www.example.com/posts/$id")
56+
->assertStatus(200)
57+
->json();
58+
59+
$this->assertSelfLink("http://www.example.com/posts/$id", $json);
60+
}
61+
62+
/**
63+
* If there is no URL namespace, the URL must be properly formed.
64+
*/
65+
public function testRequestResourceHasEmptyUrlNamespace()
66+
{
67+
$id = factory(Post::class)->create()->getKey();
68+
config()->set('json-api-default.url.namespace', '');
69+
70+
$json = $this
71+
->withApiRoutes()
72+
->getJsonApi("http://www.example.com/posts/$id")
73+
->assertStatus(200)
74+
->json();
75+
76+
$this->assertSelfLink("http://www.example.com/posts/$id", $json);
77+
}
78+
79+
/**
80+
* If there is no URL host and namespace, the URL must be properly formed.
81+
*/
82+
public function testRequestResourceDoesNotHaveHostAndUrlNamespace()
83+
{
84+
$id = factory(Post::class)->create()->getKey();
85+
config()->set('json-api-default.url.host', false);
86+
config()->set('json-api-default.url.namespace', null);
87+
88+
$json = $this
89+
->withApiRoutes()
90+
->getJsonApi("http://www.example.com/posts/$id")
91+
->assertStatus(200)
92+
->json();
93+
94+
$this->assertSelfLink("/posts/$id", $json);
4195
}
4296

4397
/**
@@ -52,7 +106,7 @@ public function testSerializedResourceHasAppHost()
52106
config()->set('json-api-default.url.host', null);
53107

54108
$json = json_api('default')->encoder()->serializeData($post);
55-
$this->assertLinks($host, $post->getKey(), $json);
109+
$this->assertSelfLink("http://www.example.com/api/v1/posts/{$post->getKey()}", $json);
56110
}
57111

58112
/**
@@ -67,7 +121,7 @@ public function testSerializedResourceHasSpecificHost()
67121
config()->set('json-api-default.url.host', $host = 'http://www.example.com');
68122

69123
$json = json_api('default')->encoder()->serializeData($post);
70-
$this->assertLinks($host, $post->getKey(), $json);
124+
$this->assertSelfLink("http://www.example.com/api/v1/posts/{$post->getKey()}", $json);
71125
}
72126

73127
/**
@@ -82,20 +136,19 @@ public function testSerializedResourceDoesNotHaveAppHost()
82136
config()->set('json-api-default.url.host', false);
83137

84138
$json = json_api('default')->encoder()->serializeData($post);
85-
$this->assertLinks('', $post->getKey(), $json);
139+
$this->assertSelfLink("/api/v1/posts/{$post->getKey()}", $json);
86140
}
87141

88142
/**
89-
* @param $host
90-
* @param $id
143+
* @param $link
91144
* @param array $json
92145
*/
93-
private function assertLinks($host, $id, array $json)
146+
private function assertSelfLink($link, array $json)
94147
{
95148
$this->assertArraySubset([
96149
'data' => [
97150
'links' => [
98-
'self' => "$host/api/v1/posts/$id",
151+
'self' => $link,
99152
],
100153
],
101154
], $json);

0 commit comments

Comments
 (0)
0