title: How to patch a FastAPI Request
body: <p>I have this function that is called as a dependency in all of my APIs.</p>
  <pre class="lang-py prettyprint-override"><code>from fastapi import HTTPException, Request, status
  
  
  async def get_user_id_or_401(request: Request) -&gt; str:
      user_id: str = request.headers.get(&quot;x-cognito-user-id&quot;)
  
      if not user_id:
          raise HTTPException(
              status_code=status.HTTP_401_UNAUTHORIZED, detail=&quot;Unauthorized&quot;
          )
      return user_id
  
  </code></pre>
  <p>How do I unit test this function? My initial move was to patch <code>request.headers.get</code> but i don't know how to go about that.</p>
  <p>So, my question is, how do I patch <code>request.headers.get</code>. However, if there is a better way I can go about testing this, please tell me.</p>