Skip to content

Commit 85d9f9a

Browse files
authored
Merge pull request #39 from ryanjdew/expand-http-helper-support
Expand support for http helpers for REST tests
2 parents 7c8357f + b3da1e4 commit 85d9f9a

File tree

3 files changed

+101
-17
lines changed

3 files changed

+101
-17
lines changed

ml-unit-test-client/build.gradle

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,49 @@ if (project.hasProperty("myBintrayUser")) {
6464
}
6565
}
6666

67-
tasks.test.dependsOn mlDeploy
67+
task createHttpCredentials(type: com.marklogic.gradle.task.MarkLogicTask) {
68+
def client = getAppConfig().newAppServicesDatabaseClient("Security")
69+
String xquery = """
70+
xquery version "1.0-ml";
71+
72+
import module namespace sec = "http://marklogic.com/xdmp/security"
73+
at "/MarkLogic/security.xqy";
74+
75+
declare option xdmp:mapping "false";
76+
77+
try {
78+
sec:remove-credential("ml-unit-test-credentials")
79+
} catch (\$e) {()};
80+
81+
xquery version "1.0-ml";
82+
83+
import module namespace sec = "http://marklogic.com/xdmp/security"
84+
at "/MarkLogic/security.xqy";
85+
86+
declare option xdmp:mapping "false";
87+
88+
sec:create-credential(
89+
"ml-unit-test-credentials",
90+
"Credentials for ML Rest Helper Calls",
91+
"${mlUsername}",
92+
"${mlPassword}",
93+
(),
94+
(),
95+
fn:false(),
96+
sec:uri-credential-target("http://localhost:${mlRestPort}/.*","digest"),
97+
xdmp:default-permissions()
98+
)
99+
""";
100+
try {
101+
String result
102+
result = client.newServerEval().xquery(xquery).evalAs(String.class);
103+
if (result != null) {
104+
println result
105+
}
106+
} finally {
107+
client.release()
108+
}
109+
}
110+
111+
mlPostDeploy.dependsOn createHttpCredentials
112+
tasks.test.dependsOn mlDeploy
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import module namespace test="http://marklogic.com/roxy/test-helper" at "/test/test-helper.xqy";
2+
3+
4+
test:assert-http-get-status("v1/search?format=xml", $test:DEFAULT_HTTP_OPTIONS,200),
5+
test:assert-http-post-status("v1/search?format=xml", $test:DEFAULT_HTTP_OPTIONS, (), 200)

ml-unit-test-modules/src/main/ml-modules/root/test/test-helper.xqy

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ declare function helper:assert-equal-json($expected, $actual) {
311311
if ($expected instance of object-node()*) then
312312
if ($actual instance of object-node()*) then
313313
if (fn:count($expected) = fn:count($actual)) then
314-
if (helper:assert-equal-json-recursive($expected, $actual)) then
314+
if (helper:assert-equal-json-recursive($expected, $actual)) then
315315
helper:success()
316316
else
317317
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed", ($expected, $actual))
@@ -321,9 +321,9 @@ declare function helper:assert-equal-json($expected, $actual) {
321321
(: $actual is not object-node()* :)
322322
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed ($actual does not consist of objects)", ($expected, $actual))
323323
else if ($expected instance of map:map*) then
324-
if ($actual instance of map:map*) then
324+
if ($actual instance of map:map*) then
325325
if (fn:count($expected) = fn:count($actual)) then
326-
if (helper:assert-equal-json-recursive($expected, $actual)) then
326+
if (helper:assert-equal-json-recursive($expected, $actual)) then
327327
helper:success()
328328
else
329329
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed", ($expected, $actual))
@@ -332,9 +332,9 @@ declare function helper:assert-equal-json($expected, $actual) {
332332
else
333333
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed ($actual does not consist of objects)", ($expected, $actual))
334334
else if ($expected instance of array-node()*) then
335-
if ($actual instance of array-node()*) then
335+
if ($actual instance of array-node()*) then
336336
if (fn:count($expected) = fn:count($actual)) then
337-
if (helper:assert-equal-json-recursive($expected, $actual)) then
337+
if (helper:assert-equal-json-recursive($expected, $actual)) then
338338
helper:success()
339339
else
340340
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed", ($expected, $actual))
@@ -343,9 +343,9 @@ declare function helper:assert-equal-json($expected, $actual) {
343343
else
344344
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed ($actual does not consist of arrays)", ($expected, $actual))
345345
else if ($expected instance of document-node()) then
346-
if ($actual instance of document-node()) then
346+
if ($actual instance of document-node()) then
347347
if (fn:count($expected) = fn:count($actual)) then
348-
if (helper:assert-equal-json-recursive($expected/node(), $actual/node())) then
348+
if (helper:assert-equal-json-recursive($expected/node(), $actual/node())) then
349349
helper:success()
350350
else
351351
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed (documents not equal)", ($expected, $actual))
@@ -400,7 +400,7 @@ declare function helper:assert-equal-json-recursive($object1, $object2) as xs:bo
400400
$counts-equal and fn:not($items-equal = fn:false())
401401
case object-node() return
402402
let $m1 := fn:data($object1)
403-
let $m2 :=
403+
let $m2 :=
404404
if ($object2 instance of object-node()) then
405405
fn:data($object2)
406406
else
@@ -541,30 +541,64 @@ declare private function helper:assert-throws-error_($function as xdmp:function,
541541
}
542542
};
543543

544+
declare variable $local-url as xs:string := xdmp:get-request-protocol() || "://localhost:" || xdmp:get-request-port();
545+
declare variable $helper:DEFAULT_HTTP_OPTIONS := element xdmp-http:options {
546+
let $credential-id := xdmp:invoke-function(function() {
547+
xdmp:apply(xdmp:function(xs:QName('sec:credential-get-id'), "/MarkLogic/security.xqy"), "ml-unit-test-credentials")
548+
}, map:entry("database", xdmp:security-database()))
549+
return
550+
element xdmp-http:credential-id {$credential-id}
551+
};
552+
544553
declare function helper:easy-url($url) as xs:string
545554
{
546555
if (fn:starts-with($url, "http")) then $url
547556
else
548-
fn:concat("http://localhost:", xdmp:get-request-port(), if (fn:starts-with($url, "/")) then () else "/", $url)
557+
fn:concat($local-url, if (fn:starts-with($url, "/")) then "" else "/", $url)
549558
};
550559

551-
declare function helper:http-get($url as xs:string, $options as node()?)
560+
declare function helper:http-get($url as xs:string, $options as item()? (:as (element(xdmp-http:options)|map:map)?:))
552561
{
553-
let $uri :=
554-
if (fn:starts-with($url, "http")) then $url
555-
else
556-
fn:concat("http://localhost:", xdmp:get-request-port(), if (fn:starts-with($url, "/")) then () else "/", $url)
562+
let $uri := helper:easy-url($url)
557563
return
558564
xdmp:http-get($uri, $options)
559565
};
560566

561-
declare function helper:assert-http-get-status($url as xs:string, $options as element(xdmp-http:options), $status-code)
567+
declare function helper:assert-http-get-status($url as xs:string, $options as item()? (:as (element(xdmp-http:options)|map:map)?:), $status-code)
562568
{
563569
let $response := helper:http-get($url, $options)
564570
return
565571
test:assert-equal($status-code, fn:data($response[1]/*:code))
566572
};
567573

574+
declare function helper:http-post($url as xs:string, $options as item()? (:as (element(xdmp-http:options)|map:map)?:), $data as node()?)
575+
{
576+
let $uri := helper:easy-url($url)
577+
return
578+
xdmp:http-post($uri, $options, $data)
579+
};
580+
581+
declare function helper:assert-http-post-status($url as xs:string, $options as item()? (:as (element(xdmp-http:options)|map:map)?:), $data as node()?, $status-code)
582+
{
583+
let $response := helper:http-post($url, $options, $data)
584+
return
585+
test:assert-equal($status-code, fn:data($response[1]/*:code))
586+
};
587+
588+
declare function helper:http-put($url as xs:string, $options as item()? (:as (element(xdmp-http:options)|map:map)?:), $data as node()?)
589+
{
590+
let $uri := helper:easy-url($url)
591+
return
592+
xdmp:http-put($uri, $options, $data)
593+
};
594+
595+
declare function helper:assert-http-put-status($url as xs:string, $options as item()? (:as (element(xdmp-http:options)|map:map)?:), $data as node()?, $status-code)
596+
{
597+
let $response := helper:http-put($url, $options, $data)
598+
return
599+
test:assert-equal($status-code, fn:data($response[1]/*:code))
600+
};
601+
568602
(:~
569603
: Convenience function to remove all xml docs from the data db
570604
:)
@@ -696,4 +730,4 @@ declare function helper:strip-blanks($n as node()) {
696730
case element() return element{node-name($n)} {$n/@*, $n/node() ! helper:strip-blanks(.)}
697731
case text() return if (fn:normalize-space($n) eq '') then () else $n
698732
default return $n
699-
};
733+
};

0 commit comments

Comments
 (0)