gun_test.exs (1042B)
1 # Pleroma: A lightweight social networking server 2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> 3 # SPDX-License-Identifier: AGPL-3.0-only 4 5 defmodule Pleroma.GunTest do 6 use ExUnit.Case 7 alias Pleroma.Gun 8 9 @moduletag :integration 10 11 test "opens connection and receive response" do 12 {:ok, conn} = Gun.open('httpbin.org', 443) 13 assert is_pid(conn) 14 {:ok, _protocol} = Gun.await_up(conn) 15 ref = :gun.get(conn, '/get?a=b&c=d') 16 assert is_reference(ref) 17 18 assert {:response, :nofin, 200, _} = Gun.await(conn, ref) 19 assert json = receive_response(conn, ref) 20 21 assert %{"args" => %{"a" => "b", "c" => "d"}} = Jason.decode!(json) 22 23 {:ok, pid} = Task.start(fn -> Process.sleep(50) end) 24 25 :ok = :gun.set_owner(conn, pid) 26 27 assert :gun.info(conn).owner == pid 28 end 29 30 defp receive_response(conn, ref, acc \\ "") do 31 case Gun.await(conn, ref) do 32 {:data, :nofin, body} -> 33 receive_response(conn, ref, acc <> body) 34 35 {:data, :fin, body} -> 36 acc <> body 37 end 38 end 39 end