Skip to content

Commit 11b1b55

Browse files
committed
feat: added missing method for adding header values to the response
1 parent ea2ef06 commit 11b1b55

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/Builder.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Builder implements Responsable
1212
protected $body;
1313

1414
/**
15-
* @var Header
15+
* @var array
1616
*/
1717
protected $headers;
1818

@@ -109,6 +109,33 @@ public function status(int $status): Builder
109109
return $this;
110110
}
111111

112+
/**
113+
* Set a HTTP header value to be returned with the response
114+
*
115+
* @param string $key
116+
* @param string $value
117+
* @return $this
118+
*/
119+
public function header(string $key, string $value)
120+
{
121+
$this->headers[$key] = $value;
122+
123+
return $this;
124+
}
125+
126+
/**
127+
* Set headers that should be returned with the response
128+
*
129+
* @param array $headers
130+
* @return $this
131+
*/
132+
public function headers(array $headers)
133+
{
134+
$this->headers = array_filter($headers);
135+
136+
return $this;
137+
}
138+
112139
/**
113140
* Return a json response using the api body
114141
*

tests/BuilderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Tests;
44

5+
use Illuminate\Foundation\Testing\TestResponse;
6+
57
class BuilderTest extends TestCase
68
{
79

@@ -33,4 +35,19 @@ public function testAddMessageToResponse()
3335
$this->assertEquals(['Hello World'], $builder->body()->getMessages());
3436
}
3537

38+
public function testAddingHeaderToResponse()
39+
{
40+
$response = new TestResponse(api()->header('foo', 'bar')->respond());
41+
42+
$response->assertHeader('foo');
43+
}
44+
45+
public function testAddingMultipleHeaderToResponse()
46+
{
47+
$response = new TestResponse(api()->headers(['foo' => 'bar', 'hello' => 'world', 'invalid header'])->respond());
48+
49+
$response->assertHeader('foo');
50+
$response->assertHeader('hello');
51+
$response->assertHeaderMissing('invalid header');
52+
}
3653
}

0 commit comments

Comments
 (0)