From 308eff5bfad1b26e9da777e723052ae207185432 Mon Sep 17 00:00:00 2001 From: Oscar Otero Date: Sat, 7 Mar 2020 12:55:39 +0100 Subject: [PATCH] release v2.0.1 --- .gitignore | 1 - CHANGELOG.md | 8 ++++++++ tests/ContentTypeTest.php | 30 ++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4b317e0..364d1a4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,3 @@ vendor composer.lock coverage *.cache -.idea diff --git a/CHANGELOG.md b/CHANGELOG.md index 7949d82..ea7cc95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [2.0.1] - 2020-03-07 +### Fixed +- If no Accept header field is present, returns the default format instead a `406` response, according to the specs [#6], [#8] + ## [2.0.0] - 2019-11-29 ### Added - The array of formats passed to `ContentType` middleware can contain plain values, for example `new Middlewares\ContentType(['html', 'json'])`, so it's not required to provide the full data for each format (the list of headers, extensions, etc). @@ -83,6 +87,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## 0.1.0 - 2016-10-01 First version +[#6]: https://github.com/middlewares/negotiation/issues/6 +[#8]: https://github.com/middlewares/negotiation/issues/8 + +[2.0.1]: https://github.com/middlewares/negotiation/compare/v2.0.0...v2.0.1 [2.0.0]: https://github.com/middlewares/negotiation/compare/v1.1.0...v2.0.0 [1.1.0]: https://github.com/middlewares/negotiation/compare/v1.0.0...v1.1.0 [1.0.0]: https://github.com/middlewares/negotiation/compare/v0.5.0...v1.0.0 diff --git a/tests/ContentTypeTest.php b/tests/ContentTypeTest.php index 54ba18b..43e2d13 100644 --- a/tests/ContentTypeTest.php +++ b/tests/ContentTypeTest.php @@ -162,4 +162,34 @@ public function testCharset(array $charsets, string $accept, string $acceptChars $this->assertEquals($result, $response->getHeaderLine('Content-Type')); } + + public function testMissingHeader() + { + $request = Factory::createServerRequest('GET', '/'); + + $response = Dispatcher::run([ + (new ContentType(['json', 'html']))->errorResponse(), + function ($request) { + echo $request->getHeaderLine('Accept'); + }, + ], $request); + + $this->assertEquals('application/json', (string) $response->getBody()); + $this->assertEquals('application/json; charset=UTF-8', $response->getHeaderLine('Content-Type')); + } + + public function testNotMissingHeader() + { + $request = Factory::createServerRequest('GET', '/')->withHeader('Accept', 'image/*'); + + $response = Dispatcher::run([ + (new ContentType(['json', 'html']))->errorResponse(), + function ($request) { + echo $request->getHeaderLine('Accept'); + }, + ], $request); + + $this->assertEquals('', $response->getHeaderLine('Content-Type')); + $this->assertEquals(406, $response->getStatusCode()); + } }