Hi,
I’ve been struggling for a while with using JSON inside unit tests (or at least that seems to me to be the problem). I am new to Prolog, so I have been trying to study the SWI-Prolog documentation in detail, but something must have escaped my attention. Please forgive any inexact terms I may use.
The problem is that I receive the error “‘json_term’ expected, found … (a compound)” when I try to introduce a JSON object inside the test block (I also tried using a dict object). I have tested the same JSON object outside plunit, and it seems to be correct.
I will include the test results and code to reproduce the problem.
Any help would be greatly appreciated.
Best,
Mikko Tiihonen
Welcome to SWI-Prolog (threaded, 64 bits, version 8.0.3)
?- load_test_files([make(all)]).
**true.**
?- run_tests.
% PL-Unit: test_server
% Started server at http://localhost:50001/
**ERROR: …/test/test_server.plt:8:**
**test server: assertion failed**
**Assertion: _10130==foo**
**ERROR: …/test/test_server.plt:8:**
**test server: received error: Type error: `json_term' expected, found `param1=foo' (a compound)**
done
% 1 assertion failed
% 1 test failed
% 0 tests passed
**false.**
test_server.pl:
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/json)).
:- use_module(library(http/http_json)).
server(Port) :-
http_server(http_dispatch,
[ port(Port)
]).
:- http_handler(root(.), entry_page, []).
entry_page(Request) :-
reply(Request).
reply(Request) :-
member(method(post), Request), !,
http_read_json(Request, DataDict, [json_object(dict)]),
get_dict(param1, DataDict, ReplyDict),
reply_json_dict(ReplyDict).
test_server.plt:
:- use_module(library(http/http_client)).
:- use_module(library(http/json)).
:- include(test_server).
:- begin_tests(test_server).
test(server) :-
setup_call_cleanup(
server(50001),
http_post('http://localhost:50001', json([param1='foo', param2='bar']), In, []),
(assertion(In == 'foo'), http_stop_server(50001, []))).
:- end_tests(test_server).