Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Plug.Test.sent_chunks/2 #1160

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Commits on Oct 4, 2023

  1. Add Plug.Test.sent_chunks/2

    Before this patch there was no way to reconstruct the invidual chunks
    that were sent. All we got was the full resulting body in
    `conn.resp_body`.
    
    For completeness, I believe instead of messaging we could store chunks in a list
    in the test adapter state and simply append:
    
        - def send_chunked(state, _status, _headers), do: {:ok, "", %{state | chunks: ""}}
        + def send_chunked(state, _status, _headers), do: {:ok, "", %{state | chunks: []}}
    
        - def chunk(%{owner: owner, ref: ref, chunks: chunks} = state, chunk) do
        -   send(owner, {ref, :chunk, chunk})
        -   body = chunks <> IO.iodata_to_binary(chunk)
        -   {:ok, body, %{state | chunks: body}}
        - end
        + def chunk(%{owner: owner, ref: ref, chunks: chunks} = state, chunk) do
        +   chunk = IO.iodata_to_binary(chunk)
        +   body = IO.iodata_to_binary([chunks, chunk])
        +   {:ok, body, %{state | chunks: chunks ++ [chunk]}}
        + end
    
    but I was following the existing functions `sent_informs` and `sent_upgrades`.
    
    My use case is to be able to test response streaming using Req :plug
    adapter:
    
        req =
          Req.new(
            plug: fn conn ->
              conn = Plug.Conn.send_chunked(conn, 200)
              {:ok, conn} = Plug.Conn.chunk(conn, "foo")
              {:ok, conn} = Plug.Conn.chunk(conn, "bar")
              conn
            end,
            into: []
          )
    
        resp = Req.request!(req)
        assert resp.body == ["foo", "bar"]
    wojtekmach committed Oct 4, 2023
    Configuration menu
    Copy the full SHA
    854c18d View commit details
    Browse the repository at this point in the history

Commits on Nov 29, 2023

  1. Add test

    wojtekmach committed Nov 29, 2023
    Configuration menu
    Copy the full SHA
    951cdf6 View commit details
    Browse the repository at this point in the history