main
  1class ServerTest < Minitest::Test
  2  include Rack::Test::Methods
  3
  4  def app
  5    Server.new
  6  end
  7
  8  def test_get_graphql_with_query_string
  9    header 'Content-Type', 'application/graphql'
 10    get '/', query: '{me}'
 11
 12    assert last_response.ok?
 13    assert_equal 200, last_response.status
 14    refute_empty last_response.body
 15
 16    json = JSON.parse(last_response.body)
 17    assert_equal 'mo', json['data']['me']
 18  end
 19
 20  def test_get_graphql_with_post_body
 21    header 'Content-Type', 'application/graphql'
 22    post '/', '{me}'
 23
 24    assert last_response.ok?
 25    assert_equal 200, last_response.status
 26    refute_empty last_response.body
 27
 28    json = JSON.parse(last_response.body)
 29    assert_equal 'mo', json['data']['me']
 30  end
 31
 32  def test_get_schema
 33    header 'Content-Type', 'application/graphql'
 34    post '/', '{ __schema { types { name } } }'
 35
 36    assert last_response.ok?
 37    json = JSON.parse(last_response.body)
 38
 39    [
 40      'Boolean',
 41      'Cake',
 42      'Query',
 43      'String',
 44      '__Directive',
 45      '__DirectiveLocation',
 46      '__EnumValue',
 47      '__InputValue',
 48      '__Type',
 49      '__TypeKind',
 50    ].each do |type|
 51      assert json['data']['__schema']['types'].include?('name' => type)
 52    end
 53  end
 54
 55  def test_get_query_type
 56    header 'Content-Type', 'application/graphql'
 57    post '/', '{ __schema { queryType { name } } }'
 58
 59    assert last_response.ok?
 60    json = JSON.parse(last_response.body)
 61    assert 'Query', json['data']['__schema']['queryType']['name']
 62  end
 63
 64  def test_get_cakes_type
 65    header 'Content-Type', 'application/graphql'
 66    post '/', <<~GQL
 67    {
 68      __type(name: "Cake") {
 69        name
 70        kind
 71      }
 72    }
 73    GQL
 74
 75    assert last_response.ok?
 76    json = JSON.parse(last_response.body)
 77    assert_equal 'Cake', json['data']['__type']['name']
 78    assert_equal 'OBJECT', json['data']['__type']['kind']
 79  end
 80
 81  def test_get_cake_fields
 82    header 'Content-Type', 'application/graphql'
 83    post '/', <<~GQL
 84    {
 85      __type(name: "Cake") {
 86        name
 87        fields {
 88          name
 89          type {
 90            name
 91            kind
 92          }
 93        }
 94      }
 95    }
 96    GQL
 97
 98    assert last_response.ok?
 99    json = JSON.parse(last_response.body)
100    assert_equal 'Cake', json.dig('data', '__type', 'name')
101    assert_equal [{
102      'name' => 'name',
103      'type' => {
104        'name' => nil,
105        'kind' => 'NON_NULL'
106      }
107    }], json.dig('data', '__type', 'fields')
108  end
109end