ruby-on-rails - 对临时站点的 POST 请求如何工作?
问题描述
我正在开发一个使用 Ruby on Rails 和 GraphQL API 编写的应用程序。在我的测试文件中,我向我们的暂存站点发送了一个 POST 请求,以执行登录突变。这是测试文件:
require 'test_helper'
class Mutations::SignInUserTest < ActionDispatch::IntegrationTest
def perform(args = {})
Mutations::SignInUser.new(object: nil, field: nil, context: {}).resolve(args)
end
test 'sign in user' do
post "apistaging.app.com/graphql"
user = perform(
auth: {
user_id: "example",
password: "example"
}
)
assert user.persisted?
end
end
但是,在 中test.log
,这是我跑步时看到的rails test
Started POST "/graphql" for 127.0.0.1 at 2021-06-16 00:06:27 +0700
Processing by GraphqlController#execute as HTML
Completed 200 OK in 32ms (Views: 0.3ms | ActiveRecord: 0.0ms)
请注意,我的根目录中有一个 Dockerfile 和一个 docker-compose.yml,docker-compose up
然后运行docker exec
到我们的应用程序的容器中运行rails test
。日志是否意味着 POST 请求正在发送到我的 localhost:3000/graphql?我问是因为登台数据库应该有用于登录的用户数据,但由于某种原因,当我运行测试时数据库是空的,所以所有查询和突变都返回 null。我的根目录中还有一个 .sql 文件,有没有办法将文件中的数据导入到我的数据库中以便进行测试?
解决方案
我认为你错过的第一件事是docker-compose build
。之后你应该docker-compose up
。此外,您还必须知道您的 docker 配置到哪个端口。默认值为 80,但是您的端口可能已经在使用中,将 docker 端口保留为 80 并不是一个好习惯。您应该在您的端口中执行以下操作Dockerfile
:
EXPOSE 5050 #or any other port you want
CMD ["rails", "server", "-b", "0.0.0.0"]
EXPOSE 5050
公开本地机器上的端口 5050 供 docker 使用
CMD ["rails", "server", "-b", "0.0.0.0"]
,让您可以使用其网络中的任何 IP 地址,您不必担心管理 IP 地址
在你的docker-compose.yml
:
your-api:
ports:
- 5050:5050
因此,当docker-compose build
运行时,它将组成您的 docker 并打开端口 5050 供其使用,这样您就可以访问localhost:5050
并访问您的 dockerised api 和端点。
这样你就可以确定你的 POST 请求被发送到 docker api 当你POST localhost:5050/graphql
推荐阅读
- java - 自 Andriod 11 以来应用程序活动上方的黑色死区
- python - 将多个值包装在单个 JSON 中并存储在模型中 - Django
- c# - 列出当前目录中某个文件夹中的某些项目
- python - 在 python tkinter 中制作游戏计时器的问题
- c - 在另一个结构中构造 tm 时间
- javascript - 将 GET 对象推送到数据返回中的空对象
- serial-port - 如何使用 Delphi 编写代码以通过 RS232 接收数据计数?
- arrays - 如何制作解密字符串的ruby方法?
- c - 纸牌问题,C 函数有问题
- performance - Neo4j 3.5 查询性能问题