ecal_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. "testing"
  13. "devt.de/krotik/eliasdb/api"
  14. )
  15. func TestECAL(t *testing.T) {
  16. internalURL := "http://localhost" + TESTPORT + EndpointECALInternal
  17. publicURL := "http://localhost" + TESTPORT + EndpointECALPublic
  18. // Test normal log output
  19. writeScript(`
  20. log("test insert")
  21. `)
  22. if err := api.SI.Run(); err != nil {
  23. t.Error("Unexpected result:", err)
  24. return
  25. }
  26. if err := checkLog(`test insert
  27. `); err != nil {
  28. t.Error(err)
  29. }
  30. writeScript(`
  31. log("test sinks")
  32. sink mysink
  33. kindmatch [ "db.web.api" ],
  34. statematch { "method" : "POST", "path" : "xx/ss" }
  35. {
  36. del(event.state.header, "Accept-Encoding")
  37. del(event.state.header, "User-Agent")
  38. log("Got public web request: ", event)
  39. log("Body data: ", event.state.bodyJSON.data)
  40. db.raiseWebEventHandled({
  41. "status" : 201,
  42. "body" : {
  43. "mydata" : [1,2,3]
  44. }
  45. })
  46. }
  47. sink mysink2
  48. kindmatch [ "db.web.ecal" ],
  49. statematch { "method" : "GET" }
  50. {
  51. del(event.state.header, "Accept-Encoding")
  52. del(event.state.header, "User-Agent")
  53. log("Got internal web request: ", event)
  54. log("Query data: ", event.state.query.xxx)
  55. raise("aaa")
  56. }
  57. `)
  58. if err := api.SI.Run(); err != nil {
  59. t.Error("Unexpected result:", err)
  60. return
  61. }
  62. st, _, res := sendTestRequest(internalURL+"main/n/Test/bar?xxx=1", "GET", nil)
  63. if st != "404 Not Found" || res != "Resource was not found" {
  64. t.Error("Unexpected result:", st, res)
  65. return
  66. }
  67. st, header, res := sendTestRequest(publicURL+"xx/ss/?a=1&b=2", "POST", []byte(`
  68. {
  69. "data": 123
  70. }
  71. `[1:]))
  72. if st != "201 Created" || header["Content-Type"][0] != "application/json; charset=utf-8" || string(res) != `{
  73. "mydata": [
  74. 1,
  75. 2,
  76. 3
  77. ]
  78. }` {
  79. t.Error("Unexpected result:", st, header, string(res))
  80. return
  81. }
  82. if err := checkLog(`test sinks
  83. Got internal web request: {
  84. "kind": "db.web.ecal",
  85. "name": "WebRequest",
  86. "state": {
  87. "bodyJSON": null,
  88. "bodyString": "",
  89. "header": {
  90. "Content-Type": [
  91. "application/json"
  92. ]
  93. },
  94. "method": "GET",
  95. "path": "main/n/Test/bar",
  96. "pathList": [
  97. "main",
  98. "n",
  99. "Test",
  100. "bar"
  101. ],
  102. "query": {
  103. "xxx": [
  104. "1"
  105. ]
  106. }
  107. }
  108. }
  109. Query data: [
  110. "1"
  111. ]
  112. error: ECAL error in eliasdb-runtime (testscripts/main.ecal): aaa () (Line:26 Pos:3)
  113. Got public web request: {
  114. "kind": "db.web.api",
  115. "name": "WebRequest",
  116. "state": {
  117. "bodyJSON": {
  118. "data": 123
  119. },
  120. "bodyString": "{\n \"data\": 123\n}\n",
  121. "header": {
  122. "Content-Length": [
  123. "18"
  124. ],
  125. "Content-Type": [
  126. "application/json"
  127. ]
  128. },
  129. "method": "POST",
  130. "path": "xx/ss",
  131. "pathList": [
  132. "xx",
  133. "ss"
  134. ],
  135. "query": {
  136. "a": [
  137. "1"
  138. ],
  139. "b": [
  140. "2"
  141. ]
  142. }
  143. }
  144. }
  145. Body data: 123
  146. `); err != nil {
  147. t.Error(err)
  148. return
  149. }
  150. oldSI := api.SI
  151. defer func() {
  152. api.SI = oldSI
  153. }()
  154. api.SI = nil
  155. st, _, res = sendTestRequest(internalURL, "PUT", nil)
  156. if st != "404 Not Found" || res != "Resource was not found" {
  157. t.Error("Unexpected result:", st, res)
  158. return
  159. }
  160. st, _, res = sendTestRequest(internalURL, "DELETE", nil)
  161. if st != "404 Not Found" || res != "Resource was not found" {
  162. t.Error("Unexpected result:", st, res)
  163. return
  164. }
  165. }