How do I inspect response headers in laravel 4 for unit testing?

bigmandan

I have seen many examples on how to set headers on a response but I cannot find a way to inspect the headers of a response.

For example in a test case I have:

public function testGetJson()
{
    $response = $this->action('GET', 'LocationTypeController@index', null, array('Accept' => 'application/json'));
    $this->assertResponseStatus(200);
    //some code here to test that the response content-type is 'application/json'
}

public function testGetXml()
{
    $response = $this->action('GET', 'LocationTypeController@index', null, array('Accept' => 'text/xml'));
    $this->assertResponseStatus(200);
    //some code here to test that the response content-type is 'text/xml'
}

How would I go about testing that the content-type header is 'application/json' or any other content-type? Maybe I'm misunderstanding something?

The controllers I have can do content negation with the Accept header and I want to make sure the content type in the response is correct.

Thanks!

bigmandan

After some digging around in the Symfony and Laravel docs I was able to figure it out...

public function testGetJson()
{
    // Symfony interally prefixes headers with "HTTP", so 
    // just Accept would not work.  I also had the method signature wrong...
    $response = $this->action('GET', 'LocationTypeController@index',
        array(), array(), array(), array('HTTP_Accept' => 'application/json'));
    $this->assertResponseStatus(200);
    // I just needed to access the public
    // headers var (which is a Symfony ResponseHeaderBag object)
    $this->assertEquals('application/json', 
        $response->headers->get('Content-Type'));
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How do i unit test the auth filter in Laravel 4.1?

来自分类Dev

How do I parse a RestSharp response into a class?

来自分类Dev

Grails 3 Unit Testing: How do you do mockFor, createMock, and demands in Grails 3?

来自分类Dev

Using Dependency Injection I find myself creating factories to do Unit Testing. Is this normal?

来自分类Dev

In Python 3 how can I patch a function inside a module for unit testing?

来自分类Dev

How do I unit test a function that loops over objects?

来自分类Dev

How to mock a CakePHP behavior for unit testing

来自分类Dev

How do I override the response content in a JMeter WebDriver sampler test?

来自分类Dev

How do I patch or modify a list inside of a function when running a unit test?

来自分类Dev

When using (substack's) Tape module for testing, how do I run only one test in a file?

来自分类Dev

Python - unit testing

来自分类Dev

Unit testing with google test

来自分类Dev

How do I get a Cookie from a SoapUI response using a Groovy test step?

来自分类Dev

how do i pass the HTTP response from one activity to another activity in android

来自分类Dev

Response.Headers.Add()与Response.AddHeader()

来自分类Dev

If I do not understand the difference in stability between Debian stable and Debian testing, am I better off with Debian testing?

来自分类Dev

Jasmine unit testing an element is not :hidden

来自分类Dev

Laravel get request headers

来自分类Dev

How do I make Auto Hotkey do Alt+F4 when I press right mouse button and scroll wheel?

来自分类Dev

How do I take the last so many entries (skip) with Eloquent Query Builder in Laravel

来自分类Dev

Laravel - How do I select all where column is equals to x or y or z (and on..)?

来自分类Dev

How do you test for values created in a new Buffer in unit tests

来自分类Dev

How to do Laravel ORM subquery

来自分类Dev

HTTP response headers: multiple Link values

来自分类Dev

How can I unit test Eclipse Command Handlers?

来自分类Dev

Laravel 4 PHP致命错误:找不到类“ Illuminate \ Foundation \ Testing \ TestCase”

来自分类Dev

Modifying $scope Values for Angular Controller Unit Testing

来自分类Dev

NHibernate Unit Testing Mocking/In Memory Database

来自分类Dev

Unit Testing interactions between multiple processes

Related 相关文章

  1. 1

    How do i unit test the auth filter in Laravel 4.1?

  2. 2

    How do I parse a RestSharp response into a class?

  3. 3

    Grails 3 Unit Testing: How do you do mockFor, createMock, and demands in Grails 3?

  4. 4

    Using Dependency Injection I find myself creating factories to do Unit Testing. Is this normal?

  5. 5

    In Python 3 how can I patch a function inside a module for unit testing?

  6. 6

    How do I unit test a function that loops over objects?

  7. 7

    How to mock a CakePHP behavior for unit testing

  8. 8

    How do I override the response content in a JMeter WebDriver sampler test?

  9. 9

    How do I patch or modify a list inside of a function when running a unit test?

  10. 10

    When using (substack's) Tape module for testing, how do I run only one test in a file?

  11. 11

    Python - unit testing

  12. 12

    Unit testing with google test

  13. 13

    How do I get a Cookie from a SoapUI response using a Groovy test step?

  14. 14

    how do i pass the HTTP response from one activity to another activity in android

  15. 15

    Response.Headers.Add()与Response.AddHeader()

  16. 16

    If I do not understand the difference in stability between Debian stable and Debian testing, am I better off with Debian testing?

  17. 17

    Jasmine unit testing an element is not :hidden

  18. 18

    Laravel get request headers

  19. 19

    How do I make Auto Hotkey do Alt+F4 when I press right mouse button and scroll wheel?

  20. 20

    How do I take the last so many entries (skip) with Eloquent Query Builder in Laravel

  21. 21

    Laravel - How do I select all where column is equals to x or y or z (and on..)?

  22. 22

    How do you test for values created in a new Buffer in unit tests

  23. 23

    How to do Laravel ORM subquery

  24. 24

    HTTP response headers: multiple Link values

  25. 25

    How can I unit test Eclipse Command Handlers?

  26. 26

    Laravel 4 PHP致命错误:找不到类“ Illuminate \ Foundation \ Testing \ TestCase”

  27. 27

    Modifying $scope Values for Angular Controller Unit Testing

  28. 28

    NHibernate Unit Testing Mocking/In Memory Database

  29. 29

    Unit Testing interactions between multiple processes

热门标签

归档