123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- /*
- * EliasDB
- *
- * Copyright 2016 Matthias Ladkau. All rights reserved.
- *
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/.
- */
- package v1
- import (
- "encoding/json"
- "net/url"
- "testing"
- "devt.de/krotik/common/errorutil"
- )
- func TestGraphQLQuery(t *testing.T) {
- queryURL := "http://localhost" + TESTPORT + EndpointGraphQLQuery
- query := url.QueryEscape(`{
- Song(key : "Aria1") {
- key
- }
- }`)
- _, _, res := sendTestRequest(queryURL+"main?query="+query, "GET", nil)
- if res != `
- {
- "data": {
- "Song": [
- {
- "key": "Aria1"
- }
- ]
- }
- }`[1:] {
- t.Error("Unexpected response:", res)
- return
- }
- query = url.QueryEscape(`query foo($bar : String) {
- Song(key : $bar) {
- key
- }
- }`)
- variables := url.QueryEscape(`{ "bar" : "Aria1" }`)
- _, _, res = sendTestRequest(queryURL+"main?operationName=foo&query="+query+"&variables="+variables, "GET", nil)
- if res != `
- {
- "data": {
- "Song": [
- {
- "key": "Aria1"
- }
- ]
- }
- }`[1:] {
- t.Error("Unexpected response:", res)
- return
- }
- }
- func TestGraphQLQueryErrors(t *testing.T) {
- queryURL := "http://localhost" + TESTPORT + EndpointGraphQLQuery
- query := url.QueryEscape(`{`)
- _, _, res := sendTestRequest(queryURL+"main?query="+query, "GET", nil)
- if res != "Parse error in Main query: Unexpected end (Line:1 Pos:1)" {
- t.Error("Unexpected response:", res)
- return
- }
- _, _, res = sendTestRequest(queryURL+"?query="+query, "GET", nil)
- if res != "Need a partition" {
- t.Error("Unexpected response:", res)
- return
- }
- _, _, res = sendTestRequest(queryURL+"main?ry="+query, "GET", nil)
- if res != "Need a query parameter" {
- t.Error("Unexpected response:", res)
- return
- }
- _, _, res = sendTestRequest(queryURL+"main?query="+query+"&variables=123", "GET", nil)
- if res != "Could not decode variables: json: cannot unmarshal number into Go value of type map[string]interface {}" {
- t.Error("Unexpected response:", res)
- return
- }
- }
- func TestGraphQL(t *testing.T) {
- queryURL := "http://localhost" + TESTPORT + EndpointGraphQL
- q, err := json.Marshal(map[string]interface{}{
- "partition": "main",
- "query": `{
- Song(key : "Aria1") {
- key
- }
- }`,
- })
- errorutil.AssertOk(err)
- _, _, res := sendTestRequest(queryURL+"main", "POST", q)
- if res != `
- {
- "data": {
- "Song": [
- {
- "key": "Aria1"
- }
- ]
- }
- }`[1:] {
- t.Error("Unexpected response:", res)
- return
- }
- }
- func TestGraphQLErrors(t *testing.T) {
- queryURL := "http://localhost" + TESTPORT + EndpointGraphQL
- q, err := json.Marshal(map[string]interface{}{
- "operationName": nil,
- "variables": nil,
- "query": "{",
- })
- errorutil.AssertOk(err)
- _, _, res := sendTestRequest(queryURL+"main", "POST", q)
- if res != "Parse error in Main query: Unexpected end (Line:1 Pos:1)" {
- t.Error("Unexpected response:", res)
- return
- }
- q, err = json.Marshal(map[string]interface{}{
- "operationName": nil,
- "variables": nil,
- "query": "{",
- })
- errorutil.AssertOk(err)
- _, _, res = sendTestRequest(queryURL, "POST", q)
- if res != "Need a partition" {
- t.Error("Unexpected response:", res)
- return
- }
- q, err = json.Marshal(map[string]interface{}{
- "partition": "main",
- "operationName": nil,
- "variables": nil,
- })
- errorutil.AssertOk(err)
- _, _, res = sendTestRequest(queryURL, "POST", q)
- if res != "Mandatory field 'query' missing from query object" {
- t.Error("Unexpected response:", res)
- return
- }
- _, _, res = sendTestRequest(queryURL, "POST", []byte("{"))
- if res != "Could not decode request body: unexpected EOF" {
- t.Error("Unexpected response:", res)
- return
- }
- }
- func TestGraphQLParsing(t *testing.T) {
- queryURL := "http://localhost" + TESTPORT + EndpointGraphQL
- q, err := json.Marshal(map[string]interface{}{
- "query-to-ast": `{
- Song
- }`,
- })
- errorutil.AssertOk(err)
- _, _, res := sendTestRequest(queryURL+"main", "POST", q)
- if res != `
- {
- "result-ast": {
- "children": [
- {
- "children": [
- {
- "children": [
- {
- "children": [
- {
- "children": [
- {
- "name": "Name",
- "value": "Song"
- }
- ],
- "name": "Field"
- }
- ],
- "name": "SelectionSet"
- }
- ],
- "name": "OperationDefinition"
- }
- ],
- "name": "ExecutableDefinition"
- }
- ],
- "name": "Document"
- }
- }`[1:] {
- t.Error("Unexpected response:", res)
- return
- }
- _, _, res = sendTestRequest(queryURL+"main", "POST", []byte(`{"ast-to-query": {
- "children": [
- {
- "children": [
- {
- "children": [
- {
- "children": [
- {
- "children": [
- {
- "name": "Name",
- "value": "Song"
- }
- ],
- "name": "Field"
- }
- ],
- "name": "SelectionSet"
- }
- ],
- "name": "OperationDefinition"
- }
- ],
- "name": "ExecutableDefinition"
- }
- ],
- "name": "Document"
- }}`))
- if res != `
- {
- "result-query": "{\n Song\n}"
- }`[1:] {
- t.Error("Unexpected response:", res)
- return
- }
- }
- func TestGraphQLParsingErrors(t *testing.T) {
- queryURL := "http://localhost" + TESTPORT + EndpointGraphQL
- q, err := json.Marshal(map[string]interface{}{
- "query-to-ast": `{{
- Song
- }`,
- })
- errorutil.AssertOk(err)
- _, _, res := sendTestRequest(queryURL+"main", "POST", q)
- if res != "Parse error in request: Name expected ({) (Line:1 Pos:2)" {
- t.Error("Unexpected response:", res)
- return
- }
- q, err = json.Marshal(map[string]interface{}{
- "ast-to-query": `aaa`,
- })
- errorutil.AssertOk(err)
- _, _, res = sendTestRequest(queryURL+"main", "POST", q)
- if res != "Plain AST object expected as 'ast-to-query' value" {
- t.Error("Unexpected response:", res)
- return
- }
- q, err = json.Marshal(map[string]interface{}{
- "ast-to-query": map[string]interface{}{
- "foo": `Document`,
- },
- })
- errorutil.AssertOk(err)
- _, _, res = sendTestRequest(queryURL+"main", "POST", q)
- if res != "Found plain ast node without a name: map[foo:Document]" {
- t.Error("Unexpected response:", res)
- return
- }
- q, err = json.Marshal(map[string]interface{}{
- "ast-to-query": map[string]interface{}{
- "name": `foo`,
- },
- })
- errorutil.AssertOk(err)
- _, _, res = sendTestRequest(queryURL+"main", "POST", q)
- if res != "Could not find template for foo (tempkey: foo)" {
- t.Error("Unexpected response:", res)
- return
- }
- }
|