package com.blazemeter.api.http;

import com.blazemeter.api.BlazeMeterReport;
import kg.apc.jmeter.reporters.notifier.StatusNotifierCallbackTest;
import net.sf.json.JSONObject;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.junit.Test;

import static org.junit.Assert.*;


public class BlazeMeterHttpUtilsTest {

    @Test
    public void testFlow() throws Exception {
        final StatusNotifierCallbackTest.StatusNotifierCallbackImpl callbackTest = new StatusNotifierCallbackTest.StatusNotifierCallbackImpl();
        final String address = "http://ip.jsontest.com/";
        BlazeMeterReport report = new BlazeMeterReport();
        report.setToken("test_token");
        BlazeMeterHttpUtils entity = new BlazeMeterHttpUtils(callbackTest, address, address, report);

        assertEquals(callbackTest, entity.getNotifier());
        assertEquals(address, entity.getAddress());
        assertEquals(address, entity.getDataAddress());
        assertEquals(report, entity.getReport());
        assertNotNull(entity.getHttpClient());

        HttpGet get = entity.createGet(address);
        JSONObject response = entity.queryObject(get, 200);
        assertEquals("test_token", get.getHeaders("X-Api-Key")[0].getValue());
        assertTrue(response.containsKey("ip"));

        report.setToken("test:token");
        entity = new BlazeMeterHttpUtils(callbackTest, address, address, report);
        HttpPost post = entity.createPost(address, "");
        entity.queryObject(post, 200);
        assertTrue(post.getHeaders("Authorization")[0].getValue().startsWith("Basic "));
        assertTrue(response.containsKey("ip"));
    }

    @Test
    public void extractErrorMessageTest() throws Exception {
        final StatusNotifierCallbackTest.StatusNotifierCallbackImpl callbackTest = new StatusNotifierCallbackTest.StatusNotifierCallbackImpl();
        final String address = "http://ip.jsontest.com/";
        BlazeMeterReport report = new BlazeMeterReport();
        report.setToken("test_token");
        BlazeMeterHttpUtils entity = new BlazeMeterHttpUtils(callbackTest, address, address, report);

        String errorResponse = "{\"error\":{\"message\":\"Please, try later!\"}}";
        String message = entity.extractErrorMessage(errorResponse);

        assertEquals("Please, try later!", message);

        errorResponse = "Please, try later!";
        assertEquals(errorResponse, entity.extractErrorMessage(errorResponse));
    }
}
@@ -162,22 +162,113 @@
 son.JSON
-Object
+;%0Aimport net.sf.json.JSONObject;%0Aimport net.sf.json.JSONSerializer;%0Aimport net.sf.json.JsonConfig
 ;%0Aimport
@@ -371,18 +371,102 @@
 org.
-junit.Test
+apache.http.client.methods.HttpRequestBase;%0Aimport org.junit.Test;%0A%0Aimport java.io.IOException
 ;%0A%0Ai
@@ -772,39 +772,26 @@
 ress = %22
-http://ip.jsontest.com/
+empty_host
 %22;%0A     
@@ -919,34 +919,26 @@
 ntity = 
-new BlazeMeterHttp
+createMock
 Utils(ca
@@ -953,33 +953,24 @@
 st, address,
- address,
  report);%0A%0A 
@@ -1457,26 +1457,84 @@
 ontainsKey(%22
-ip
+message%22));%0A        assertEquals(%22ok%22, response.get(%22message
 %22));%0A%0A      
@@ -1583,34 +1583,26 @@
 ntity = 
-new BlazeMeterHttp
+createMock
 Utils(ca
@@ -1617,33 +1617,24 @@
 st, address,
- address,
  report);%0A  
@@ -1860,13 +1860,915 @@
 ey(%22
-ip%22))
+message%22));%0A        assertEquals(%22ok%22, response.get(%22message%22));%0A    %7D%0A%0A    private BlazeMeterHttpUtils createMockUtils(StatusNotifierCallbackTest.StatusNotifierCallbackImpl callbackTest,%0A                                                String address, BlazeMeterReport report) %7B%0A        return new BlazeMeterHttpUtils(callbackTest, address, address, report) %7B%0A            @Override%0A            public JSON query(HttpRequestBase request, int expectedCode) throws IOException %7B%0A                try %7B%0A                    super.query(request, expectedCode);%0A                    fail(%22Cannot send request to %22 + address);%0A                %7D catch (IllegalStateException ex) %7B%0A                    assertEquals(%22Target host must not be null, or set in parameters.%22, ex.getMessage());%0A                %7D%0A                return JSONSerializer.toJSON(%22%7B%5C%22message%5C%22 : %5C%22ok%5C%22%7D%22, new JsonConfig());%0A            %7D%0A        %7D
 ;%0A  
@@ -3023,31 +3023,17 @@
  = %22
-http://ip.jsontest.com/
+localhost
 %22;%0A 

fix sense-test (#32)