Skip to content

Commit f310060

Browse files
committed
middleware keep content-type to json when return string
1 parent c417200 commit f310060

3 files changed

Lines changed: 51 additions & 18 deletions

File tree

src/Action.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ public function run()
153153
$returned = call_user_func($callable);
154154

155155
if(is_array($returned)) {
156-
$app->response->json($returned);
156+
$app->response->json($returned, null, $app->response->getContentType());
157157
} elseif(is_string($returned)) {
158-
$app->response->html($returned);
158+
$app->response->html($returned, null, $app->response->getContentType());
159159
}
160160

161161
return $app->response->body;

src/Http/Response.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function isStatus($status_code)
127127
*/
128128
public function setContentType($type)
129129
{
130-
$this->header["CONTENT_TYPE"] = $type;
130+
$this->headers["CONTENT_TYPE"] = $type;
131131
return $this;
132132
}
133133

@@ -138,22 +138,22 @@ public function setContentType($type)
138138
*/
139139
public function getContentType()
140140
{
141-
return $this->header["CONTENT_TYPE"];
141+
return $this->headers["CONTENT_TYPE"];
142142
}
143143

144-
public function json(array $data, $status = null)
144+
public function json(array $data, $status = null, $content_type = null)
145145
{
146146
$json = json_encode($data);
147-
$this->setContentType(static::CONTENT_TYPE_JSON);
147+
$this->setContentType($content_type ? $content_type : static::CONTENT_TYPE_JSON);
148148
$this->setStatus($status);
149149
$this->body = $json;
150150

151151
return $this;
152152
}
153153

154-
public function html($content, $status = null)
154+
public function html($content, $status = null, $content_type = null)
155155
{
156-
$this->setContentType(static::CONTENT_TYPE_HTML);
156+
$this->setContentType($content_type ? $content_type : static::CONTENT_TYPE_HTML);
157157
$this->setStatus($status);
158158
$this->body = $content;
159159

@@ -175,7 +175,6 @@ public function reset()
175175
$this->has_sent = false;
176176

177177
return $this
178-
->setContentType(static::CONTENT_TYPE_HTML)
179178
->setStatus(200)
180179
->clean();
181180
}
@@ -228,14 +227,11 @@ public function getStatusMessage($status_code)
228227
protected function writeHeaders()
229228
{
230229
$headers = $this->headers->all(false);
231-
$content_type = $this->getContentType();
230+
$content_type = ($type = $this->getContentType()) ? $type : static::CONTENT_TYPE_HTML;
232231

233232
// http://stackoverflow.com/questions/6163970/set-response-status-code
234233
header("HTTP/1.1 ".$this->getStatusMessage($this->status), true, $this->status);
235-
if ($content_type) {
236-
header('Content-type: '.$content_type);
237-
}
238-
234+
header('Content-type: '.$content_type);
239235
foreach($headers as $key => $value) {
240236
$header = $this->normalizeHeaderKey($key).': '.$value;
241237
header($header);

tests/RunAppTests.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function testMultipleMiddleware()
273273
return $request->str."bazQux";
274274
})->setStr('foobar')->jsonify()->uppercase();
275275

276-
$this->assertResponse("GET", "/foo", '{"body":"FOOBARBAZQUX"}');
276+
$this->assertResponse("GET", "/foo", '{"body":"FOOBARBAZQUX"}', 200, 'application/json');
277277
}
278278

279279
/**
@@ -310,6 +310,41 @@ public function testRouteGroup()
310310
$this->assertResponse("GET", "/group/hello", 'IM IN GROUP');
311311
}
312312

313+
/**
314+
* @runInSeparateProcess
315+
* @preserveGlobalState enabled
316+
*/
317+
public function testResponseJson()
318+
{
319+
$this->app->get('/anything.json', function() {
320+
return [
321+
'message' => 'hello'
322+
];
323+
});
324+
325+
$this->assertResponse("GET", "/anything.json", '{"message":"hello"}', 200, 'application/json');
326+
}
327+
328+
/**
329+
* @runInSeparateProcess
330+
* @preserveGlobalState enabled
331+
*/
332+
public function testMiddlewareKeepResponseToJson()
333+
{
334+
$this->app->middleware('uppercase', function($req, $res, $next) {
335+
$next();
336+
return strtoupper($res->body);
337+
});
338+
339+
$this->app->get('/anything.json', function() {
340+
return [
341+
'message' => 'hello'
342+
];
343+
})->uppercase();
344+
345+
$this->assertResponse("GET", "/anything.json", '{"MESSAGE":"HELLO"}', 200, 'application/json');
346+
}
347+
313348
/**
314349
* @runInSeparateProcess
315350
* @preserveGlobalState enabled
@@ -427,12 +462,14 @@ protected function runAndGetResponse($method, $path)
427462
return [$response, $rendered];
428463
}
429464

430-
protected function assertResponse($method, $path, $assert_body, $assert_status = 200)
465+
protected function assertResponse($method, $path, $assert_body, $assert_status = 200, $assert_content_type = 'text/html')
431466
{
467+
$at = $method.' '.$path.' => '.$assert_body;
432468
list($response, $rendered) = $this->runAndGetResponse($method, $path);
433469

434-
$this->assertEquals($rendered, $assert_body);
435-
$this->assertEquals($response->getStatus(), $assert_status);
470+
$this->assertEquals($rendered, $assert_body, $at);
471+
$this->assertEquals($response->getStatus(), $assert_status, $at);
472+
$this->assertEquals($response->getContentType(), $assert_content_type, $at);
436473
}
437474

438475
}

0 commit comments

Comments
 (0)