POSTing imposter using Java code

501 views
Skip to first unread message

Shekhar Bhardwaj

unread,
Dec 29, 2015, 10:06:36 AM12/29/15
to mountebank-discuss
Hi,

   I am trying to create an imposter using java, but it's failing with an exception, some how it's working fine for HTML and XML types but not with JSON, need help, below is the JSON, JAVA code and Exception.


my imposter -

 {
  "port": "9091",
  "protocol": "http",
  "stubs": [
    {
      "responses": [
        {
          "is": {
            "headers": {
               "Content-Type": "application/json"
            },
            "statusCode": 200,
            "body": "{
                      \"type\": \"simpleRefNo\"

              }"
          }
        }
      ],
      "predicates": [
        {
          "and": [
            {
              "equals": {
                "method": "GET"
              }
            },
            {
              "contains": {
                "path": "fortune_cookie"
              }
            },
            {
              "deepEquals": {
                "query": {
                  "type": "latest"
                }
              }
            }
          ]
        }
      ]
    }
  ]
}

my Java code -

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

import javax.net.ssl.HttpsURLConnection;

public class Test {
        
        public static void main(String[] args) throws Exception {
                
                 String t =getStringFromInputStream( );
                // System.out.println(t);
                 sendPost( t);
                
        }

        private static String getStringFromInputStream( ) throws IOException {

                // The name of the file to open.
        
        Path path = FileSystems.getDefault().getPath("//TestFactory//mbjson//", "newres1.json");

        String contents  = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
                return contents;
    }
        
        
        
        
        private static void sendPost(String urlParameter) throws Exception {

                String url = "http://127.0.0.1:2525/imposters";
                URL obj = new URL(url);
                HttpURLConnection con = (HttpURLConnection) obj.openConnection();

                //add reuqest header
                con.setRequestMethod("POST");
                con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
                con.setRequestProperty("Content-Type","application/json");

                String urlParameters = urlParameter;
                // Send post request
                con.setDoOutput(true);
                DataOutputStream wr = new DataOutputStream(con.getOutputStream());
                wr.writeBytes(urlParameters);
                wr.flush();
                wr.close();

                int responseCode = con.getResponseCode();
                System.out.println("\nSending 'POST' request to URL : " + url);
                System.out.println("Post parameters : " + urlParameters);
                System.out.println("Response Code : " + responseCode);

                BufferedReader in = new BufferedReader(
                        new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                }
                in.close();
                
                //print result
                System.out.println(response.toString());

        }

}
        

Exception I'm facing -

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: http://127.0.0.1:2525/imposters

Shekhar Bhardwaj

unread,
Dec 29, 2015, 10:10:26 AM12/29/15
to mountebank-discuss
it's working with post man, SOAPUI and CURL, I was going through Java example given on MounteBank portal but it seems that's just for TCP.

Shekhar Bhardwaj

unread,
Dec 29, 2015, 10:16:06 AM12/29/15
to mountebank-discuss
Same code is working fine for this example -

Sending 'POST' request to URL : http://127.0.0.1:2525/imposters

Post parameters : {

  "port": "3400",

  "protocol": "http",

  "stubs": [

    {

      "responses": [

        {

          "is": {

            "headers": {

              "Content-type": "text/html"

            },

            "statusCode": 200,

            "body": "get a job"

          }

        }

      ],

      "predicates": [

        {

          "and": [

            {

              "equals": {

                "method": "GET"

              }

            },

            {

              "contains": {

                "path": "fortune_cookie"

              }

            },

            {

              "deepEquals": {

                "query": {

                  "type": "latest"

                }

              }

            }

          ]

        }

      ]

    }

  ]

}


Response Code : 201



Brandon Byars

unread,
Dec 30, 2015, 12:11:27 PM12/30/15
to Shekhar Bhardwaj, mountebank-discuss
Hi there,
It would help if you capture the HTTP response body - it should provide some more clues.  Looking over your JSON, you have some illegal newlines in there:


--
You received this message because you are subscribed to the Google Groups "mountebank-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brandon Byars

unread,
Dec 30, 2015, 12:12:50 PM12/30/15
to Shekhar Bhardwaj, mountebank-discuss
Sorry, pressed send too soon.  Look at the JSON, I see an illegal newline here:

"body": "{
                      \"type\": \"simpleRefNo\"

              }"


Proper JSON would be:


"body": "{\n\"type\": \"simpleRefNo\"\n}"

The next release will accept pure JSON objects for the body field, making that easier to type.  I'll release it as soon as I can (recovering from surgery at the moment).

-Brandon


Shekhar Bhardwaj

unread,
Jan 4, 2016, 5:38:57 PM1/4/16
to mountebank-discuss
Thanks, Got it now!

  I am also going through JavaBank, just wanted to ask if the IsBuilder is in upcoming release?

 

jmth...@thoughtworks.com

unread,
Jan 5, 2016, 12:07:52 PM1/5/16
to mountebank-discuss
Hey Shekhar,

I'm the author of Javabank. Unfortunately I haven't really had time to work on it lately, but I thought the IsBuilder was in the currently released version. Could you tell me what version you're using? Did you build it from the source on github? Or did you get it from Nexus?

If you got it from Nexus, it's probably just that I need to do an updated release, which I should at least be able to find the time for.

- James
Reply all
Reply to author
Forward
0 new messages