Mock WebService Response

Former Member
Former Member
I'm trying to connect to a webservice that returns a payload with a slightly different format depending on the time of day (inorite?). Is there a way of mocking the response in a file so that I can test it without having to wait or do I have to input the entire payload into code? Tried searching forums and docs but to no avail.

Kit
  • What I've done in the past with json data, is it put a file on a web server (mydata.txt for example), and use the url of that file for the testing. Sometimes I'd get data using the real url in a browser, and just copy/paste it to the file on the web server.
  • If you use python, you can use the built-in web server for testing like this.

    I create a folder that contains files all of the various files documents I want to retrieve, and then start the SimpleHTTPServer in that folder. For example, here is a test directory that I have for verifying FIT/GPX file fetching.

    C:\Users\Travis\Desktop\ConnectIQ\webserver>dir /b
    COURSES.FIT
    TRACK.GPX
    WAYPOINT.GPX
    WAYPOINT_TRACK.GPX
    WORKOUT.FIT

    C:\Users\Travis\Desktop\ConnectIQ\webserver>python -m SimpleHTTPServer 8080
    Serving HTTP on 0.0.0.0 port 8080 ...


    If you fetch (using the simulator) the url http://localhost:8080/TRACK.GPX, the built-in python web server will serve that file up for you.

    Travis
  • Former Member
    Former Member over 8 years ago
    Thanks for the advice, I was looking for a built-in mock service since there's a test suite but I guess I'll launch an external webserver.

    If you use python, you can use the built-in web server for testing like this.

    I create a folder that contains files all of the various files documents I want to retrieve, and then start the SimpleHTTPServer in that folder. For example, here is a test directory that I have for verifying FIT/GPX file fetching.

    C:\Users\Travis\Desktop\ConnectIQ\webserver>dir /b
    COURSES.FIT
    TRACK.GPX
    WAYPOINT.GPX
    WAYPOINT_TRACK.GPX
    WORKOUT.FIT

    C:\Users\Travis\Desktop\ConnectIQ\webserver>python -m SimpleHTTPServer 8080
    Serving HTTP on 0.0.0.0 port 8080 ...


    If you fetch (using the simulator) the url http://localhost:8080/TRACK.GPX, the built-in python web server will serve that file up for you.

    Travis
  • There are a few services out there that do this:

    * http://www.mocky.io/
    * http://putsreq.com/
    * http://wiremock.org/
    * https://apiary.io/

    I've used the first two, and they're both easy to set up and use. We used to use PutsReq to test our Communication module, but now we use a local Python service like Travis suggested