Subiendo api v2
This commit is contained in:
		
							
								
								
									
										32
									
								
								deps/phoenix/priv/templates/phx.gen.channel/channel.ex
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								deps/phoenix/priv/templates/phx.gen.channel/channel.ex
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,32 @@
 | 
			
		||||
defmodule <%= module %>Channel do
 | 
			
		||||
  use <%= web_module %>, :channel
 | 
			
		||||
 | 
			
		||||
  @impl true
 | 
			
		||||
  def join("<%= singular %>:lobby", payload, socket) do
 | 
			
		||||
    if authorized?(payload) do
 | 
			
		||||
      {:ok, socket}
 | 
			
		||||
    else
 | 
			
		||||
      {:error, %{reason: "unauthorized"}}
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  # Channels can be used in a request/response fashion
 | 
			
		||||
  # by sending replies to requests from the client
 | 
			
		||||
  @impl true
 | 
			
		||||
  def handle_in("ping", payload, socket) do
 | 
			
		||||
    {:reply, {:ok, payload}, socket}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  # It is also common to receive messages from the client and
 | 
			
		||||
  # broadcast to everyone in the current topic (<%= singular %>:lobby).
 | 
			
		||||
  @impl true
 | 
			
		||||
  def handle_in("shout", payload, socket) do
 | 
			
		||||
    broadcast(socket, "shout", payload)
 | 
			
		||||
    {:noreply, socket}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  # Add authorization logic here as required.
 | 
			
		||||
  defp authorized?(_payload) do
 | 
			
		||||
    true
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										39
									
								
								deps/phoenix/priv/templates/phx.gen.channel/channel_case.ex
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								deps/phoenix/priv/templates/phx.gen.channel/channel_case.ex
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,39 @@
 | 
			
		||||
defmodule <%= web_module %>.ChannelCase do
 | 
			
		||||
  @moduledoc """
 | 
			
		||||
  This module defines the test case to be used by
 | 
			
		||||
  channel tests.
 | 
			
		||||
 | 
			
		||||
  Such tests rely on `Phoenix.ChannelTest` and also
 | 
			
		||||
  import other functionality to make it easier
 | 
			
		||||
  to build common data structures and query the data layer.
 | 
			
		||||
 | 
			
		||||
  Finally, if the test case interacts with the database,
 | 
			
		||||
  we enable the SQL sandbox, so changes done to the database
 | 
			
		||||
  are reverted at the end of every test. If you are using
 | 
			
		||||
  PostgreSQL, you can even run database tests asynchronously
 | 
			
		||||
  by setting `use <%= web_module %>.ChannelCase, async: true`, although
 | 
			
		||||
  this option is not recommended for other databases.
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use ExUnit.CaseTemplate
 | 
			
		||||
 | 
			
		||||
  using do
 | 
			
		||||
    quote do
 | 
			
		||||
      # Import conveniences for testing with channels
 | 
			
		||||
      import Phoenix.ChannelTest
 | 
			
		||||
      import <%= web_module %>.ChannelCase
 | 
			
		||||
 | 
			
		||||
      # The default endpoint for testing
 | 
			
		||||
      @endpoint <%= web_module %>.Endpoint
 | 
			
		||||
    end
 | 
			
		||||
  end<%= if Code.ensure_loaded?(Ecto.Adapters.SQL) do %>
 | 
			
		||||
 | 
			
		||||
  setup tags do
 | 
			
		||||
    <%= base %>.DataCase.setup_sandbox(tags)
 | 
			
		||||
    :ok
 | 
			
		||||
  end<% else %>
 | 
			
		||||
 | 
			
		||||
  setup _tags do
 | 
			
		||||
    :ok
 | 
			
		||||
  end<% end %>
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										27
									
								
								deps/phoenix/priv/templates/phx.gen.channel/channel_test.exs
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								deps/phoenix/priv/templates/phx.gen.channel/channel_test.exs
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
			
		||||
defmodule <%= module %>ChannelTest do
 | 
			
		||||
  use <%= web_module %>.ChannelCase
 | 
			
		||||
 | 
			
		||||
  setup do
 | 
			
		||||
    {:ok, _, socket} =
 | 
			
		||||
      <%= web_module %>.UserSocket
 | 
			
		||||
      |> socket("user_id", %{some: :assign})
 | 
			
		||||
      |> subscribe_and_join(<%= module %>Channel, "<%= singular %>:lobby")
 | 
			
		||||
 | 
			
		||||
    %{socket: socket}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  test "ping replies with status ok", %{socket: socket} do
 | 
			
		||||
    ref = push(socket, "ping", %{"hello" => "there"})
 | 
			
		||||
    assert_reply ref, :ok, %{"hello" => "there"}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  test "shout broadcasts to <%= singular %>:lobby", %{socket: socket} do
 | 
			
		||||
    push(socket, "shout", %{"hello" => "all"})
 | 
			
		||||
    assert_broadcast "shout", %{"hello" => "all"}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  test "broadcasts are pushed to the client", %{socket: socket} do
 | 
			
		||||
    broadcast_from!(socket, "broadcast", %{"some" => "data"})
 | 
			
		||||
    assert_push "broadcast", %{"some" => "data"}
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Reference in New Issue
	
	Block a user