annoviko / robotframework-httpctrl

HttpCtrl is a library for Robot Framework that provides API of HTTP(S) client and server
https://annoviko.github.io/robotframework-httpctrl/
BSD 3-Clause "New" or "Revised" License
13 stars 2 forks source link

It should be possible to use 'Reply By' before waiting for any request #30

Closed ghilainm closed 2 years ago

ghilainm commented 2 years ago

Is your feature request related to a problem? Please describe. I would like to define in advance a response to a future request.

Example:

Describe the solution you'd like I should be able to define a Reply By for any future request matching some criteria.

Describe alternatives you've considered Using another library that seems to implement this: https://github.com/ravihuang/robotframework-stublibrary

ghilainm commented 2 years ago

In fact I just gave it a try and it works like described above. However, it is not what is written in the doc. Is it because it does not work all the time?

Send response using specified HTTP code and body. This function should be called after Wait For Request.

annoviko commented 2 years ago

Hello @ghilainm ,

Thanks for the issue. It makes sense to reply to a specific request. But I need a bit more information. As I understand you want to implement the following behavior, correct me if I am wrong:

  1. Initialize HTTP(S) client on robot side.
  2. Initialize HTTP(S) server on robot side.
  3. Send request by the client from the robot side.
  4. Wait for response by the client on the robot side.
  5. Wait for request (callback from your backend) by the server on the robot side.
  6. Reply to request (callback from your backend) by the server on the robot side.

Because it can be covered by the following test (haven't run this test - do not have a backend with a callback, just a draft):

*** Settings ***

Library         HttpCtrl.Client
Library         HttpCtrl.Server

Test Setup       Initialize HTTP Client And Server
Test Teardown    Terminate HTTP Server

*** Test Cases ***

Callback Test
    ${request body}=   Set Variable   { "callback": "/api/v1/callback1" }
    Send HTTP Request   POST   /api/v1/set_callback1   ${request body}

    ${response status}=   Get Response Status
    ${expected status}=   Convert To Integer   200
    Should Be Equal   ${response status}   ${expected status}

    Wait For Request
    ${url}=      Get Request Url
    Should Be Equal   ${url}      /api/v1/callback1
    Reply By   200

Or does the server receive other requests during that time? And you need something like:

Wait For Request With URI    /api/v1/callback1
Reply By   200
annoviko commented 2 years ago

@ghilainm your request is more clear for in the morning. Make sense. I think it is much better to introduce new keyword for that purpose.

ghilainm commented 2 years ago

Hello, just to make it a bit more clear, that's the kind of interaction I would like to implement.

https://kroki.io/plantuml/svg/eNpVjjEOwyAMRXdOgTIlUnOBDL1Ch6oHAOIBBRkLOz1_ISSFLNaX__vfJpPEO08GRadoo2jDVSjqrOFYzSy7HQpRxA2wxm2Aa_FOqVTtm5-1b9EMstM7J0dnQijYJ4WHTsAUkWFqgbNi0R6_cYMXQTLiI_bBSV03c6D8c9F3qDh67js5z7Ed_be0R9eIoH5ql2Pg

I don't know if it is what you understood from my original post :).

annoviko commented 2 years ago

Hi @ghilainm ,

I have introduced new keywords:

Both are available in HttpCtrl release 0.2.5:

$ pip3 install robotframework-httpctrl

There is a usage example:

*** Settings ***

Library         HttpCtrl.Client
Library         HttpCtrl.Server

Test Setup       Initialize HTTP Client And Server
Test Teardown    Terminate HTTP Server

*** Test Cases ***

Set Signle Stub And Send Request
    # Set server stub to reply automatically to POST /api/v1/post
    Set Stub Reply   POST   /api/v1/post   200   Post Message

    # Send HTTP request to the server
    Send HTTP Request   POST   /api/v1/post

    # Check that the client receives pre-defined values by the stub
    ${status}=     Get Response Status
    ${body}=       Get Response Body

    Should Be Equal   ${status}   ${200}
    Should Be Equal   ${body}     Post Message

    # Check that the server receives a single request
    ${count}=      Get Stub Count   POST   /api/v1/post
    Should Be Equal   ${count}   ${1}

*** Keywords ***

Initialize HTTP Client And Server
    Initialize Client   127.0.0.1   8000
    Start Server        127.0.0.1   8000

Terminate HTTP Server
    Stop Server
annoviko commented 2 years ago

Lets consider it as done since no feedback.

ghilainm commented 2 years ago

@annoviko Sorry mate, no time to work on this now :). Thx a lot for having a look at the proposal. I'll give it a try as soon as I can ;).