File tree Expand file tree Collapse file tree 2 files changed +45
-1
lines changed
Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff line change 22
33namespace Tests ;
44
5+ use Illuminate \Foundation \Testing \TestResponse ;
6+
57class 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}
You can’t perform that action at this time.
0 commit comments