How to post JSON data in rails 3 functional test

前端 未结 7 1840
礼貌的吻别
礼貌的吻别 2021-02-04 06:55

I plan to use JSON data in both request and response in my project and having some problems in testing.

After searching for a while, I find the following code which uses

7条回答
  •  旧巷少年郎
    2021-02-04 07:31

    Here is a snippet that let me post json data to test my own app. rails 3

    port = Rails.env.production? ? 80 : 3000
    uri = URI.parse( Rails.application.routes.url_helpers.books_url(:host => request.host, :port => port, :format => :json) )
    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Post.new(uri.request_uri)
    request.content_type = 'application/json'
    request.body = @json_data
    response = http.request( request )
    @result = response.body
    

    Hope this helps others

提交回复
热议问题