Skip to content

Instantly share code, notes, and snippets.

@LoreRizzetto
Created February 26, 2024 10:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LoreRizzetto/e2da67e438990dc55d133b88a4872525 to your computer and use it in GitHub Desktop.
Save LoreRizzetto/e2da67e438990dc55d133b88a4872525 to your computer and use it in GitHub Desktop.
Jsonapi testing in ruby
require 'json'
require "faraday"
response = Faraday.get("http://127.0.0.1:8081/products")
raise "Test fallito" unless response.status == 200
gotten = JSON.parse(response.body)
raise "Test fallito" unless gotten.has_key?("data")
raise "Test fallito" unless gotten["data"].is_a? Array
raise "Test fallito" unless gotten["data"][0].has_key?("type")
raise "Test fallito" unless gotten["data"][0].has_key?("id")
raise "Test fallito" unless gotten["data"][0].has_key?("attributes")
raise "Test fallito" unless gotten["data"][0]["attributes"].has_key?("nome")
raise "Test fallito" unless gotten["data"][0]["attributes"].has_key?("marca")
raise "Test fallito" unless gotten["data"][0]["attributes"].has_key?("prezzo")
body = '{"data": {"nome": "test_prodotto", "marca": "Marca1", "prezzo": 19}}'
response = Faraday.post("http://127.0.0.1:8081/products", body)
raise "Test fallito" unless response.status == 201
gotten = JSON.parse(response.body)
raise "Test fallito" unless gotten.has_key?("data")
raise "Test fallito" unless gotten["data"].has_key?("type")
raise "Test fallito" unless gotten["data"].has_key?("id")
raise "Test fallito" unless gotten["data"].has_key?("attributes")
raise "Test fallito" unless gotten["data"]["attributes"].has_key?("nome")
raise "Test fallito" unless gotten["data"]["attributes"].has_key?("marca")
raise "Test fallito" unless gotten["data"]["attributes"].has_key?("prezzo")
test_id = gotten["data"]["id"]
response = Faraday.get("http://127.0.0.1:8081/products/#{test_id}")
raise "Test fallito" unless response.status == 200
gotten = JSON.parse(response.body)
raise "Test fallito" unless gotten.has_key?("data")
raise "Test fallito" unless gotten["data"].is_a? Array
raise "Test fallito" unless gotten["data"].has_key?("type")
raise "Test fallito" unless gotten["data"].has_key?("id")
raise "Test fallito" unless gotten["data"].has_key?("attributes")
raise "Test fallito" unless gotten["data"]["attributes"].has_key?("nome")
raise "Test fallito" unless gotten["data"]["attributes"].has_key?("marca")
raise "Test fallito" unless gotten["data"]["attributes"].has_key?("prezzo")
["nome", "marca"].each do |key|
# body = "{\"data\": {\"#{key}\": \"test\"}}"
body = {}
body["data"] = {}
body["data"][key] = "test"
response = Faraday.patch("http://127.0.0.1:8081/products/#{test_id}", JSON.generate(body))
raise "Test fallito" unless response.status == 200
gotten = JSON.parse(response.body)
raise "Test fallito" unless gotten.has_key?("data")
raise "Test fallito" unless gotten["data"].has_key?("type")
raise "Test fallito" unless gotten["data"].has_key?("id")
raise "Test fallito" unless gotten["data"].has_key?("attributes")
raise "Test fallito" unless gotten["data"]["attributes"].has_key?("nome")
raise "Test fallito" unless gotten["data"]["attributes"].has_key?("marca")
raise "Test fallito" unless gotten["data"]["attributes"].has_key?("prezzo")
raise "Test fallito" if gotten["data"]["attributes"][key] != "test"
end
body = {}
body["data"] = {}
body["data"]["prezzo"] = 64
response = Faraday.patch("http://127.0.0.1:8081/products/#{test_id}", JSON.generate(body))
raise "Test fallito" unless response.status == 200
gotten = JSON.parse(response.body)
raise "Test fallito" unless gotten.has_key?("data")
raise "Test fallito" unless gotten["data"].has_key?("type")
raise "Test fallito" unless gotten["data"].has_key?("id")
raise "Test fallito" unless gotten["data"].has_key?("attributes")
raise "Test fallito" unless gotten["data"]["attributes"].has_key?("nome")
raise "Test fallito" unless gotten["data"]["attributes"].has_key?("marca")
raise "Test fallito" unless gotten["data"]["attributes"].has_key?("prezzo")
raise "Test fallito" if gotten["data"]["attributes"]["prezzo"] != 64
response = Faraday.delete("http://127.0.0.1:8081/products/#{test_id}")
raise "Test fallito" unless response.status == 204
response = Faraday.delete("http://127.0.0.1:8081/products/#{test_id}")
raise "Test fallito" unless response.status == 404
@LoreRizzetto
Copy link
Author

LoreRizzetto commented Feb 26, 2024

require "rack/test"
require "json"
require "faraday"



RSpec.describe "Test", :type => :request do
  include Rack::Test::Methods
  def app
    lambda do |env|
      p env
      response = Faraday.send(
        env["REQUEST_METHOD"].downcase, 
        "http://127.0.0.1:8081#{env['PATH_INFO']}?#{env['QUERY_STRING']}", 
        ["POST", "PUT", "PATCH"].include?(env["REQUEST_METHOD"]) ? env["rack.input"].string : nil
      )
      [response.status, {'content-type' => 'text/plain'}, [response.body]]
    end
  end

  describe "GET /products" do
    before (:all) do
      get "/products"
    end

    it { expect(last_response.status).to eq(200) }
    it { JSON.parse(last_response.body) }
  end

  describe "POST /products" do
    before (:all) do
      post "/products", '{...}'
    end

    it { expect(last_response.status).to eq(201) }
    it { expect(last_response.body).to eq("") }
  end

end

@LoreRizzetto
Copy link
Author

LoreRizzetto commented Feb 26, 2024

si potrebbe usare faraday al posto di rack test così (evitando l'abominio di def app)

require "rack/test"
require "json"
require "faraday"



RSpec.describe "Test", :type => :request do
  include Rack::Test::Methods
  def app
    lambda do |env|
      p env
      response = Faraday.send(
        env["REQUEST_METHOD"].downcase, 
        "http://127.0.0.1:8081#{env['PATH_INFO']}?#{env['QUERY_STRING']}", 
        ["POST", "PUT", "PATCH"].include?(env["REQUEST_METHOD"]) ? env["rack.input"].string : nil
      )
      [response.status, {'content-type' => 'text/plain'}, [response.body]]
    end
  end

  describe "GET /products" do
    before (:all) do
      #get "/products"
      @response = Faraday.get("http://127.0.0.1:8081/products")
    end

    it { expect(@response.status).to eq(200) }
    it { JSON.parse(@response.body) }
  end

  describe "POST /products" do
    before (:all) do
      post "/products", '{...}'
    end

    it { expect(last_response.status).to eq(201) }
    it { expect(last_response.body).to eq("") }
  end

end

@cristianogregnanin
Copy link

manca ancora da parametrizzare :id nell'url

require "json"
require "faraday"

addresses = ["127.0.0.1"]

addresses.each do |address|
RSpec.describe "Test", :type => :request do

describe "GET /products" do
  before (:each) do
    @response = Faraday.get("http://#{address}:8081/products")
  end

  it { expect(@response.status).to eq(200) }
  it { JSON.parse(@response.body) }
end

describe "POST /products" do
  before (:each) do
    @response = Faraday.post("http://#{address}:8081/products")
  end

  it { expect(@response.status).to eq(201) }
end


describe "GET /products/:id" do
  before (:each) do
    @response = Faraday.get("http://#{address}:8081/products/:id")
  end

  it { expect(@response.status).to eq(200) }
end


describe "PATCH /products/:id" do
  before (:each) do
    @response = Faraday.patch("http://#{address}:8081/products/:id")
  end

  it { expect(@response.status).to eq(200) }
end



describe "DELETE /products/:id" do
  before (:each) do
    @response = Faraday.delete("http://#{address}:8081/products/:id")
  end

  it { expect(@response.status).to eq(204) }
  it { expect(@response.status).to eq(404) }
end

end
end

@cristianogregnanin
Copy link

require "json"
require "faraday"

addresses = ["bore.pub"]
port = 17499

RSpec.configure do |config|
config.before(:suite) do
@id
@random_price = rand(10..10000)
@Body = { "data": { "type": "products", "attributes": { "marca": "Adidas#{@random_price}", "nome": "superstar#{@random_price}", "prezzo": @random_price } } }
end
end

addresses.each do |address|
RSpec.describe "Test", :type => :request do

describe "POST /products" do
  before (:each) do

    @response = Faraday.post("http://#{address}:#{port}/products", JSON.generate(@body))
    @json_response = JSON.parse(@response.body)
    @id = @json_response["data"]["id"]
  end

  it { expect(@response.status).to eq(201) }
  it { expect(@json_response["data"]).not_to be_a(Array) }
  it { expect(@json_response["data"]).to be_a(Hash) }
  it { expect(@json_response["data"]).to have_key("type") }
  it { expect(@json_response["data"]["type"]).to eq("products") }
  it { expect(@json_response["data"]).to have_key("id") }
  it { expect(@json_response["data"]).to have_key("attributes") }

  it { expect(@json_response["data"]["attributes"]).to have_key("nome") }
  it { expect(@json_response["data"]["attributes"]).to have_key("marca") }
  it { expect(@json_response["data"]["attributes"]).to have_key("prezzo") }

  it { expect(@json_response["data"]["attributes"]["nome"]).to eq(@body["data"]["attributes"]["nome"]) }
  it { expect(@json_response["data"]["attributes"]["marca"]).to eq(@body["data"]["attributes"]["marca"]) }
  it { expect(@json_response["data"]["attributes"]["prezzo"]).to eq(@body["data"]["attributes"]["prezzo"]) }

end

describe "GET /products" do
  before (:each) do
    @response = Faraday.get("http://#{address}:#{port}/products")
    @json_response = JSON.parse(@response.body)
  end

  it { expect(@response.status).to eq(200) }
  it { expect(@json_response["data"]).to be_a(Array) }
  it { expect(@json_response["data"][0]).to have_key("type") }
  it { expect(@json_response["data"][0]["type"]).to eq("products") }
  it { expect(@json_response["data"][0]).to have_key("id") }
  it { expect(@json_response["data"][0]).to have_key("attributes") }

  it { expect(@json_response["data"][0]["attributes"]).to have_key("nome") }
  it { expect(@json_response["data"][0]["attributes"]).to have_key("marca") }
  it { expect(@json_response["data"][0]["attributes"]).to have_key("prezzo") }

end

describe "GET /products/#{@id}" do
  before (:each) do
    @response = Faraday.get("http://#{address}:#{port}/products/#{@id}")
    @json_response = JSON.parse(@response.body)
  end

  it { expect(@response.status).to eq(200) }
  it { expect(@json_response["data"]).not_to be_a(Array) }
  it { expect(@json_response["data"]).to be_a(Hash) }
  it { expect(@json_response["data"]).to have_key("type") }
  it { expect(@json_response["data"]["type"]).to eq("products") }
  it { expect(@json_response["data"]).to have_key("id") }
  it { expect(@json_response["data"]).to have_key("attributes") }

  it { expect(@json_response["data"]["id"]).to eq(@id) }

  it { expect(@json_response["data"]["attributes"]).to have_key("nome") }
  it { expect(@json_response["data"]["attributes"]).to have_key("marca") }
  it { expect(@json_response["data"]["attributes"]).to have_key("prezzo") }

  it { expect(@json_response["data"]["attributes"]["nome"]).to eq(@body["data"]["attributes"]["nome"]) }
  it { expect(@json_response["data"]["attributes"]["marca"]).to eq(@body["data"]["attributes"]["marca"]) }
  it { expect(@json_response["data"]["attributes"]["prezzo"]).to eq(@body["data"]["attributes"]["prezzo"]) }

end

describe "PATCH /products/#{@id}" do
  before (:each) do
    @patch_body = { "data": { "type": "products", "attributes": { "marca": "nuova_marca", "nome": "nuovo_nome", "prezzo": 5 } } }

    @response = Faraday.patch("http://#{address}:#{port}/products/#{@id}", JSON.generate(@patch_body))
    @json_response = JSON.parse(@response.body)

  end

  it { expect(@response.status).to eq(200) }

  it { expect(@json_response["data"]).not_to be_a(Array) }
  it { expect(@json_response["data"]).to be_a(Hash) }
  it { expect(@json_response["data"]).to have_key("type") }
  it { expect(@json_response["data"]["type"]).to eq("products") }
  it { expect(@json_response["data"]).to have_key("id") }
  it { expect(@json_response["data"]).to have_key("attributes") }

  it { expect(@json_response["data"]["id"]).to eq(@id) }

  it { expect(@json_response["data"]["attributes"]).to have_key("nome") }
  it { expect(@json_response["data"]["attributes"]).to have_key("marca") }
  it { expect(@json_response["data"]["attributes"]).to have_key("prezzo") }

  it { expect(@json_response["data"]["attributes"]["nome"]).to eq(@patch_body["data"]["attributes"]["nome"]) }
  it { expect(@json_response["data"]["attributes"]["marca"]).to eq(@patch_body["data"]["attributes"]["marca"]) }
  it { expect(@json_response["data"]["attributes"]["prezzo"]).to eq(@patch_body["data"]["attributes"]["prezzo"]) }

end

describe "DELETE /products/#{@id}" do
  before (:each) do
    @response = Faraday.delete("http://#{address}:#{port}/products/#{@id}")
  end

  it { expect(@response.status).to eq(204) }
  it { expect(@response.status).to eq(404) }
end

end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment