graphql_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package v1
  11. import (
  12. "encoding/json"
  13. "net/url"
  14. "testing"
  15. "devt.de/krotik/common/errorutil"
  16. )
  17. func TestGraphQLQuery(t *testing.T) {
  18. queryURL := "http://localhost" + TESTPORT + EndpointGraphQLQuery
  19. query := url.QueryEscape(`{
  20. Song(key : "Aria1") {
  21. key
  22. }
  23. }`)
  24. _, _, res := sendTestRequest(queryURL+"main?query="+query, "GET", nil)
  25. if res != `
  26. {
  27. "data": {
  28. "Song": [
  29. {
  30. "key": "Aria1"
  31. }
  32. ]
  33. }
  34. }`[1:] {
  35. t.Error("Unexpected response:", res)
  36. return
  37. }
  38. query = url.QueryEscape(`query foo($bar : String) {
  39. Song(key : $bar) {
  40. key
  41. }
  42. }`)
  43. variables := url.QueryEscape(`{ "bar" : "Aria1" }`)
  44. _, _, res = sendTestRequest(queryURL+"main?operationName=foo&query="+query+"&variables="+variables, "GET", nil)
  45. if res != `
  46. {
  47. "data": {
  48. "Song": [
  49. {
  50. "key": "Aria1"
  51. }
  52. ]
  53. }
  54. }`[1:] {
  55. t.Error("Unexpected response:", res)
  56. return
  57. }
  58. }
  59. func TestGraphQLQueryErrors(t *testing.T) {
  60. queryURL := "http://localhost" + TESTPORT + EndpointGraphQLQuery
  61. query := url.QueryEscape(`{`)
  62. _, _, res := sendTestRequest(queryURL+"main?query="+query, "GET", nil)
  63. if res != "Parse error in Main query: Unexpected end (Line:1 Pos:1)" {
  64. t.Error("Unexpected response:", res)
  65. return
  66. }
  67. _, _, res = sendTestRequest(queryURL+"?query="+query, "GET", nil)
  68. if res != "Need a partition" {
  69. t.Error("Unexpected response:", res)
  70. return
  71. }
  72. _, _, res = sendTestRequest(queryURL+"main?ry="+query, "GET", nil)
  73. if res != "Need a query parameter" {
  74. t.Error("Unexpected response:", res)
  75. return
  76. }
  77. _, _, res = sendTestRequest(queryURL+"main?query="+query+"&variables=123", "GET", nil)
  78. if res != "Could not decode variables: json: cannot unmarshal number into Go value of type map[string]interface {}" {
  79. t.Error("Unexpected response:", res)
  80. return
  81. }
  82. }
  83. func TestGraphQL(t *testing.T) {
  84. queryURL := "http://localhost" + TESTPORT + EndpointGraphQL
  85. q, err := json.Marshal(map[string]interface{}{
  86. "partition": "main",
  87. "query": `{
  88. Song(key : "Aria1") {
  89. key
  90. }
  91. }`,
  92. })
  93. errorutil.AssertOk(err)
  94. _, _, res := sendTestRequest(queryURL+"main", "POST", q)
  95. if res != `
  96. {
  97. "data": {
  98. "Song": [
  99. {
  100. "key": "Aria1"
  101. }
  102. ]
  103. }
  104. }`[1:] {
  105. t.Error("Unexpected response:", res)
  106. return
  107. }
  108. }
  109. func TestGraphQLErrors(t *testing.T) {
  110. queryURL := "http://localhost" + TESTPORT + EndpointGraphQL
  111. q, err := json.Marshal(map[string]interface{}{
  112. "operationName": nil,
  113. "variables": nil,
  114. "query": "{",
  115. })
  116. errorutil.AssertOk(err)
  117. _, _, res := sendTestRequest(queryURL+"main", "POST", q)
  118. if res != "Parse error in Main query: Unexpected end (Line:1 Pos:1)" {
  119. t.Error("Unexpected response:", res)
  120. return
  121. }
  122. q, err = json.Marshal(map[string]interface{}{
  123. "operationName": nil,
  124. "variables": nil,
  125. "query": "{",
  126. })
  127. errorutil.AssertOk(err)
  128. _, _, res = sendTestRequest(queryURL, "POST", q)
  129. if res != "Need a partition" {
  130. t.Error("Unexpected response:", res)
  131. return
  132. }
  133. q, err = json.Marshal(map[string]interface{}{
  134. "partition": "main",
  135. "operationName": nil,
  136. "variables": nil,
  137. })
  138. errorutil.AssertOk(err)
  139. _, _, res = sendTestRequest(queryURL, "POST", q)
  140. if res != "Mandatory field 'query' missing from query object" {
  141. t.Error("Unexpected response:", res)
  142. return
  143. }
  144. _, _, res = sendTestRequest(queryURL, "POST", []byte("{"))
  145. if res != "Could not decode request body: unexpected EOF" {
  146. t.Error("Unexpected response:", res)
  147. return
  148. }
  149. }