|
| 1 | +require 'minitest/autorun' |
| 2 | +require "minitest/spec" |
| 3 | + |
| 4 | +require 'graphql_server' |
| 5 | +require 'rack/test' |
| 6 | +require 'graphql' |
| 7 | +require 'hello_world_server' |
| 8 | + |
| 9 | +describe GraphqlServer do |
| 10 | + include Rack::Test::Methods |
| 11 | + |
| 12 | + describe "app" do |
| 13 | + describe "#call" do |
| 14 | + let(:app) { HelloWorldServer.app } |
| 15 | + |
| 16 | + it 'responds to a graphql query via get' do |
| 17 | + get '/', {'query' => '{ hello }'} |
| 18 | + assert_equal 200, last_response.status |
| 19 | + expected_body = '{"data":{"hello":"world"}}' |
| 20 | + assert_equal expected_body, last_response.body |
| 21 | + end |
| 22 | + |
| 23 | + it 'responds to a graphql query via post' do |
| 24 | + post '/', {'query' => '{ hello }'}.to_json |
| 25 | + assert_equal 200, last_response.status |
| 26 | + expected_body = '{"data":{"hello":"world"}}' |
| 27 | + assert_equal expected_body, last_response.body |
| 28 | + end |
| 29 | + |
| 30 | + it 'does not support PUT queries' do |
| 31 | + put '/', {'query' => '{ hello }'} |
| 32 | + assert_equal 405, last_response.status |
| 33 | + assert_equal 'GraphQL Server supports only GET/POST requests', last_response.body |
| 34 | + end |
| 35 | + |
| 36 | + it 'does not support DELETE queries' do |
| 37 | + delete '/', {'query' => '{ hello }'} |
| 38 | + assert_equal 405, last_response.status |
| 39 | + assert_equal 'GraphQL Server supports only GET/POST requests', last_response.body |
| 40 | + end |
| 41 | + |
| 42 | + it 'returns error 400 when post body is missing' do |
| 43 | + post '/' |
| 44 | + assert_equal 400, last_response.status |
| 45 | + assert_equal 'POST body missing', last_response.body |
| 46 | + end |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + describe "middleware" do |
| 51 | + describe "#call" do |
| 52 | + let(:app) { HelloWorldServer.middleware } |
| 53 | + |
| 54 | + it 'responds to a graphql query via get' do |
| 55 | + get '/', {'query' => '{ hello }'} |
| 56 | + assert_equal 200, last_response.status |
| 57 | + expected_body = '{"data":{"hello":"world"}}' |
| 58 | + assert_equal expected_body, last_response.body |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | +end |
0 commit comments