AshJsonApi.Test post function does not allow change host

Hi, sorry, I think the AshJsonApi.Test post function dose not allow to overwrite the host as a header, is it bug? or i am writing host in bad place? like:
headers = [{"x-no-tenant", "false"}, {"host", "test.example.com"}]
url = "/auth/sign-in#{filds}"

response =
@api_domain
|> post(url, %{data: data}, router: @router, headers: headers, status: 201)
|> assert_data_matches(%{
"type" => "user",
"id" => ^user_id,
"attributes" => %{"email" => ^user_email}
})
headers = [{"x-no-tenant", "false"}, {"host", "test.example.com"}]
url = "/auth/sign-in#{filds}"

response =
@api_domain
|> post(url, %{data: data}, router: @router, headers: headers, status: 201)
|> assert_data_matches(%{
"type" => "user",
"id" => ^user_id,
"attributes" => %{"email" => ^user_email}
})
it always return host example.com
** (Plug.Conn.InvalidHeaderError) set the host header with %Plug.Conn{conn | host: "example.com"}
** (Plug.Conn.InvalidHeaderError) set the host header with %Plug.Conn{conn | host: "example.com"}
Thank you in advance
Solution:
You can pass a tenant option
Jump to solution
6 Replies
ZachDaniel
ZachDaniel2mo ago
I think that's showing you how to set the host That seems like a Phoenix thing not an Ash thing Although I guess what you're saying is we don't give you access to the conn to modify it before the request?
Shahryar
ShahryarOP2mo ago
Hmmm 🤔, I just want to change it or force to change because this my project uses multi tendency and i want to test multi host in my test for example with conn test i use like this
conn =
build_conn()
|> Map.put(:host, "test.example.com")
|> put_req_header("x-no-tenant", "false")
|> put_req_header("content-type", "application/vnd.api+json")
|> put_req_header("accept", "application/vnd.api+json")
|> post("/api/json/auth/sign-in", %{
"data" => %{
"type" => "user",
"attributes" => %{
"email" => to_string(master_user.email),
"password" => @password
}
}
})
conn =
build_conn()
|> Map.put(:host, "test.example.com")
|> put_req_header("x-no-tenant", "false")
|> put_req_header("content-type", "application/vnd.api+json")
|> put_req_header("accept", "application/vnd.api+json")
|> post("/api/json/auth/sign-in", %{
"data" => %{
"type" => "user",
"attributes" => %{
"email" => to_string(master_user.email),
"password" => @password
}
}
})
Solution
ZachDaniel
ZachDaniel2mo ago
You can pass a tenant option
Shahryar
ShahryarOP2mo ago
Thank you and Sorry i will test it ♥️🙏🏻, but before that i think the Ash Json testing post function circumvents the api router pipe line , So my custom plug is not called; Am i right?
pipeline :api do
plug :accepts, ["json"]
plug :load_from_bearer
plug :set_actor, :user
plug MishkaCms.Runtime.Plugs.ApiTenantLookupPlug
end
pipeline :api do
plug :accepts, ["json"]
plug :load_from_bearer
plug :set_actor, :user
plug MishkaCms.Runtime.Plugs.ApiTenantLookupPlug
end
ZachDaniel
ZachDaniel2mo ago
You are correct IIRC
Shahryar
ShahryarOP2mo ago
So for testing some part of my app i need to use
use Phoenix.ConnTest
import Plug.Test
use Phoenix.ConnTest
import Plug.Test
instead of ash json testing Thank you sorry for disrupting

Did you find this page helpful?