[英]Can I use rack-test for deployment tests?
I've built a simple api in Sinatra
, with the purpose of setting up a deployment pipeline using Docker
. 我已经在Sinatra
构建了一个简单的api,目的是使用Docker
设置部署管道。 I'm at a stage where I could easily switch Sinatra
for something else and there's currently one reason why I might do so. 我处于一个可以轻松地将Sinatra
其他东西的阶段,目前有一个原因可以这么做。
I once wrote an api using Express
and it was trivial to reuse the tests to test a deployment: 我曾经使用Express
写过一个api,重用测试来测试部署很简单:
# Testing the code
chai.request(app)
.get('/')
# Testing a deployment
chai.request('http://localhost:8080')
.get('/')
Examples from: https://github.com/chaijs/chai-http#integration-testing 来自以下示例的示例: https : //github.com/chaijs/chai-http#integration-testing
Now I am wondering if I can accomplish the same with rack-test
and Sinatra
. 现在,我想知道是否可以通过rack-test
和Sinatra
完成相同的任务。 Simply sending a URL, instead of the app, crashes. 只是发送一个URL(而不是应用程序)会崩溃。 So is there an easy way to accomplish this? 那么有没有简单的方法可以做到这一点? I suppose I could write a testing framework on top of rack-test
, but I'm not sure it's worth it, even though I do prefer Ruby
over Javascript
and Sinatra
over Express
. 我想我可以在rack-test
之上编写一个测试框架,但是我不确定它是否值得,即使我比Javascript
更喜欢Ruby
和Express
而不是Sinatra
。
I realised that I should be able to write a rack app that forwards all requests to the environment I want to run deployment tests against. 我意识到我应该能够编写一个机架应用程序,将所有请求转发到我要针对其运行部署测试的环境。 So I went to Google and found a gem that does just that: rack-proxy
因此,我去了Google,发现了一个可以做到这一点的宝石: rack-proxy
Here's how to write a simple rack app that redirects requests to your server: 这是编写简单的机架应用程序的方法,该应用程序将请求重定向到您的服务器:
require 'rack/proxy'
class Foo < Rack::Proxy
def rewrite_env(env)
env["HTTP_HOST"] = "api.example.com"
env
end
end
Then I ran my tests against Foo.new
they succeeded. 然后我对Foo.new
进行了测试。新测试成功了。 I checked the logs of that environment and I can confirm that my tests were in fact running against that environment. 我检查了该环境的日志,并且可以确认我的测试实际上是针对该环境运行的。
Foo
may not be the best name for such a proxy and you may not want the host name hardcoded, but I'm sure you can figure out how to make this work in your project if you need it. Foo
可能不是这种代理的最佳名称,并且您可能不希望对主机名进行硬编码,但是我敢肯定,如果需要的话,您可以弄清楚如何在您的项目中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.