From f24af975d4483da33e09e33b6895aea50737726a Mon Sep 17 00:00:00 2001 From: Nesbert Hidalgo Date: Mon, 18 Feb 2013 18:00:13 -0800 Subject: [PATCH] Allow the "Accept" header to be set and override default behavior. --- src/Httpful/Request.php | 15 +++++++++------ tests/Httpful/HttpfulTest.php | 12 ++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Httpful/Request.php b/src/Httpful/Request.php index c3aa183..444c57b 100644 --- a/src/Httpful/Request.php +++ b/src/Httpful/Request.php @@ -764,14 +764,17 @@ public function _curlPrep() $headers[] = "Content-Type: {$this->content_type}"; - // http://pretty-rfc.herokuapp.com/RFC2616#header.accept - $accept = 'Accept: */*; q=0.5, text/plain; q=0.8, text/html;level=3;'; + // allow custom Accept header if set + if (!isset($this->headers['Accept'])) { + // http://pretty-rfc.herokuapp.com/RFC2616#header.accept + $accept = 'Accept: */*; q=0.5, text/plain; q=0.8, text/html;level=3;'; - if (!empty($this->expected_type)) { - $accept .= "q=0.9, {$this->expected_type}"; - } + if (!empty($this->expected_type)) { + $accept .= "q=0.9, {$this->expected_type}"; + } - $headers[] = $accept; + $headers[] = $accept; + } foreach ($this->headers as $header => $value) { $headers[] = "$header: $value"; diff --git a/tests/Httpful/HttpfulTest.php b/tests/Httpful/HttpfulTest.php index 72cbe4d..ac2ab54 100644 --- a/tests/Httpful/HttpfulTest.php +++ b/tests/Httpful/HttpfulTest.php @@ -184,6 +184,18 @@ function testAccept() $r->_curlPrep(); $this->assertContains('application/json', $r->raw_headers); } + + function testCustomAccept() + { + $accept = 'application/api-1.0+json'; + $r = Request::get('http://example.com/') + ->addHeader('Accept', $accept); + + $r->_curlPrep(); + $this->assertContains($accept, $r->raw_headers); + $this->assertEquals($accept, $r->headers['Accept']); + } + function testUserAgent() { $r = Request::get('http://example.com/')