现在你的代码内容: 使用Ruby on Rails在Swagger中返回PDF的测试。

huangapple go评论60阅读模式
英文:

test returning pdf with swagger in ruby on rails

问题

我试图测试一个返回PDF文件的端点。

我的操作如下:

def pdf
  respond_to do |format|
    format.pdf do
      @pdf = ApplicationController.render pdf: some_number,
                                          template: "show",
                                          layout: nil
      send_data(@pdf, filename: "something.pdf", type: "application/pdf", disposition: 'attachment')
    end
  end
end

我的测试代码如下:

path "/pdf" do
  get "return PDF" do
    produces 'application/pdf'

    response '200', 'PDF generated' do
      schema type: :file

      before do |example|
        sign_in @some_user
        submit_request(example.metadata)
      end

      it 'returns a 200 response' do |example|
        assert_response_matches_metadata(example.metadata)
      end
    end
  end
end

但我收到错误消息:ActionController::UnknownFormat

我正在使用:ruby 3.1.0rails 7.0.0rswag 2.8.0openapi 3.0.1

有人能帮我解决问题吗?正确的测试PDF返回的方法是什么?

英文:

I'm trying to test an endpoint that returns a pdf file.

My action looks like this:

    def pdf
     respond_to do |format|
      format.pdf do
        @pdf = ApplicationController.render pdf: some_number,
                                template: "show",
                                layout: nil
        send_data(@pdf, :filename => "something.pdf", :type => "application/pdf", disposition: 'attachment')
      end
     end
   end

and my test code looks like this:

  path "/pdf" do
    get "return PDF" do
      produces 'application/pdf'

      response '200', 'PDF generated' do
        schema type: :file

        before do |example|
          sign_in @some_user
          submit_request(example.metadata)
        end

        it 'returns a 200 response' do |example|
          assert_response_matches_metadata(example.metadata)
        end
      end
    end
  end

but I'm getting the error: ActionController::UnknownFormat

I'm using: ruby 3.1.0, rails 7.0.0, rswag 2.8.0, openapi 3.0.1

Can anyone help me with my issue? and what is the correct way to test returning pdf?

答案1

得分: 0

我通过在URL中添加“.pdf”来解决了这个问题:

path "/pdf.pdf" do
   ...
end
英文:

Simply I solved the issue by adding .pdf to the URL:

path "/pdf.pdf" do
   ...
end

huangapple
  • 本文由 发表于 2023年4月6日 21:20:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定