graphql_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. }
  150. func TestGraphQLParsing(t *testing.T) {
  151. queryURL := "http://localhost" + TESTPORT + EndpointGraphQL
  152. q, err := json.Marshal(map[string]interface{}{
  153. "query-to-ast": `{
  154. Song
  155. }`,
  156. })
  157. errorutil.AssertOk(err)
  158. _, _, res := sendTestRequest(queryURL+"main", "POST", q)
  159. if res != `
  160. {
  161. "result-ast": {
  162. "children": [
  163. {
  164. "children": [
  165. {
  166. "children": [
  167. {
  168. "children": [
  169. {
  170. "children": [
  171. {
  172. "name": "Name",
  173. "value": "Song"
  174. }
  175. ],
  176. "name": "Field"
  177. }
  178. ],
  179. "name": "SelectionSet"
  180. }
  181. ],
  182. "name": "OperationDefinition"
  183. }
  184. ],
  185. "name": "ExecutableDefinition"
  186. }
  187. ],
  188. "name": "Document"
  189. }
  190. }`[1:] {
  191. t.Error("Unexpected response:", res)
  192. return
  193. }
  194. _, _, res = sendTestRequest(queryURL+"main", "POST", []byte(`{"ast-to-query": {
  195. "children": [
  196. {
  197. "children": [
  198. {
  199. "children": [
  200. {
  201. "children": [
  202. {
  203. "children": [
  204. {
  205. "name": "Name",
  206. "value": "Song"
  207. }
  208. ],
  209. "name": "Field"
  210. }
  211. ],
  212. "name": "SelectionSet"
  213. }
  214. ],
  215. "name": "OperationDefinition"
  216. }
  217. ],
  218. "name": "ExecutableDefinition"
  219. }
  220. ],
  221. "name": "Document"
  222. }}`))
  223. if res != `
  224. {
  225. "result-query": "{\n Song\n}"
  226. }`[1:] {
  227. t.Error("Unexpected response:", res)
  228. return
  229. }
  230. }
  231. func TestGraphQLParsingErrors(t *testing.T) {
  232. queryURL := "http://localhost" + TESTPORT + EndpointGraphQL
  233. q, err := json.Marshal(map[string]interface{}{
  234. "query-to-ast": `{{
  235. Song
  236. }`,
  237. })
  238. errorutil.AssertOk(err)
  239. _, _, res := sendTestRequest(queryURL+"main", "POST", q)
  240. if res != "Parse error in request: Name expected ({) (Line:1 Pos:2)" {
  241. t.Error("Unexpected response:", res)
  242. return
  243. }
  244. q, err = json.Marshal(map[string]interface{}{
  245. "ast-to-query": `aaa`,
  246. })
  247. errorutil.AssertOk(err)
  248. _, _, res = sendTestRequest(queryURL+"main", "POST", q)
  249. if res != "Plain AST object expected as 'ast-to-query' value" {
  250. t.Error("Unexpected response:", res)
  251. return
  252. }
  253. q, err = json.Marshal(map[string]interface{}{
  254. "ast-to-query": map[string]interface{}{
  255. "foo": `Document`,
  256. },
  257. })
  258. errorutil.AssertOk(err)
  259. _, _, res = sendTestRequest(queryURL+"main", "POST", q)
  260. if res != "Found plain ast node without a name: map[foo:Document]" {
  261. t.Error("Unexpected response:", res)
  262. return
  263. }
  264. q, err = json.Marshal(map[string]interface{}{
  265. "ast-to-query": map[string]interface{}{
  266. "name": `foo`,
  267. },
  268. })
  269. errorutil.AssertOk(err)
  270. _, _, res = sendTestRequest(queryURL+"main", "POST", q)
  271. if res != "Could not find template for foo (tempkey: foo)" {
  272. t.Error("Unexpected response:", res)
  273. return
  274. }
  275. }