title: How to simulate Accept header in a test within a Laravel 5 package?
body: <p>I'm currently building a Laravel package that injects a new method in <code>Illuminate\Http\Request</code> via <a href="https://laravel.com/api/5.2/Illuminate/Support/Traits/Macroable.html">Macros</a>. The method I'm injecting has been completed and is expected to work nicely, but I also want to test it before releasing it.</p>
  
  <p>My test requires me to change the request's <code>Accept</code> header, in order for me to see if the test is passing or no. So I have done the following to simulate the request:</p>
  
  <pre><code>// package/tests/TestCase.php
  
  namespace Vendor\Package;
  
  use Illuminate\Http\Request;
  use Orchestra\Testbench\TestCase as Orchestra;
  
  abstract class TestCase extends Orchestra
  {
      /**
       * Holds the request
       * @var Illuminate\Http\Request
       */
      protected $request;
  
      /**
       * Setup the test
       */
      public function setUp()
      {
          parent::setUp();
  
          $this-&gt;request = Request::capture();
  
          $this-&gt;request-&gt;headers-&gt;set('Accept', 'application/x-yaml');
      }
  }
  </code></pre>
  
  <p>Then in my test I use the method I'm injecting into <code>Request</code> with <code>$this-&gt;request-&gt;wantsYaml()</code> and it's always returning false since the <code>Accept</code> header is not getting set to <code>application/x-yaml</code>.</p>
  
  <pre><code>class RequestTest extends TestCase
  {
      /** @test */
      public function it_should_return_a_bool_if_page_wants_yaml_or_not()
      {
          dump($this-&gt;request-&gt;wantsYaml()); // Return false
  
          $this-&gt;assertTrue($this-&gt;request-&gt;wantsYaml()); // It fails!
      }
  }
  </code></pre>
  
  <p>How do I go on simulating the headers in a test in Laravel package development?</p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>This is my <code>YamlRequest</code> class</p>
  
  <pre><code>use Illuminate\Http\Request;
  use Illuminate\Support\Str;
  
  class YamlRequest extends Request
  {
      /**
       * Acceptable content type for YAML.
       * @var array
       */
      protected $contentTypeData = ['/x-yaml', '+x-yaml'];
  
      /**
       * Determine if the current request is asking for YAML in return.
       *
       * @return bool
       */
      public function wantsYaml()
      {
          $acceptable = $this-&gt;getAcceptableContentTypes();
  
          // If I dd($acceptable), it comes out as empty during tests!
  
          return isset($acceptable[0]) &amp;&amp; Str::contains($acceptable[0], $this-&gt;contentTypeData);
      }
  }
  </code></pre>
  
  <p>So I literally have to simulate the Accept in order to see if my <code>wantsYaml</code> method is working as expected.</p>