How can I mock a http response correctly

Friday, 13 March 2026 · 3 messages · last reply 17 Mar 2026
AN
Andreas Hengsbach | nexoma13 Mar 2026 12:30 #

Hello everybody,

I'm currently trying to mock the HTTP client's response for unit tests. In both cases, I have a sequence, but I need to handle them differently.

Why is that, and how can I correctly mock a response so that it behaves exactly like the real response?

Here's my test script:

# schnipp
declare function local:httpMockResponse() {
  (
    <http:response status="200"/>,
    <html>
      <foo>bar</foo>
    </html>
  )
};
<real>{http:send-request((),"https://nexoma.de")[2]/html/name()}</real>,
<mock>{local:httpMockResponse()[2]/html/name()}</mock>,

<mock2>{local:httpMockResponse()[2]/name()}</mock2> (: Why one level lower here? :) # schnapp

I just don't get it. :(

Best regards Andreas

AB
Andy Bunce16 Mar 2026 15:34 #

Hi Andreas,

Quoted text · Andreas Hengsbach | nexoma · 1 line
> Why one level lower here?

That level difference is often the difference between a root node and an enclosing document-node. The send-request()[2] is returning a document-node. You can mock the same as below.

/Andy

declare function local:httpMockResponse()
{
    <http:response status="200"/>,

  *  document{*
      <html>
      <foo>bar</foo>
    </html>
   }
};
Quoted text · Andreas Hengsbach | nexoma · 33 lines
On Fri, 13 Mar 2026 at 11:31, Andreas Hengsbach | nexoma via BaseX-Talk < basex-talk at mailman.uni-konstanz.de> wrote: > Hello everybody, > > I'm currently trying to mock the HTTP client's response for unit tests. > In both cases, I have a sequence, but I need to handle them differently. > > Why is that, and how can I correctly mock a response so that it behaves > exactly like the real response? > > Here's my test script: > > # schnipp > declare function local:httpMockResponse() { > ( > <http:response status="200"/>, > <html> > <foo>bar</foo> > </html> > ) > }; > <real>{http:send-request((),"https://nexoma.de")[2]/html/name()}</real>, > <mock>{local:httpMockResponse()[2]/html/name()}</mock>, > > <mock2>{local:httpMockResponse()[2]/name()}</mock2> (: Why one level lower > here? :) > # schnapp > > I just don't get it. :( > > Best regards > Andreas >
AH
Andreas Hengsbach17 Mar 2026 09:48 #

Hi Andy,

That was the key clue. 👍

My colleague just found it in the specs as well: https://expath.org/spec/http-client#d2e517

Thanks for your help.

Best regards, Andreas


⚡Generated with XQuery