/*
 * Copyright (c) 2016 NECTEC
 *   National Electronics and Computer Technology Center, Thailand
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.tanrabad.survey;


import org.junit.Test;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static org.junit.Assert.assertEquals;

public class OkHttpTest extends WireMockTestBase {

    private OkHttpClient client = new OkHttpClient();

    @Test
    public void testSuccessResponse() throws Exception {
        stubFor(get(urlEqualTo("/success"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withBody("[]")));
        Request build = new Request.Builder()
                .get()
                .url(HTTP_LOCALHOST_8089 + "/success")
                .build();
        Response res = client.newCall(build).execute();

        assertEquals(200, res.code());
        assertEquals(true, res.isSuccessful());
        assertEquals("[]", res.body().string());
    }

    @Test
    public void testUnmodifiedRequest() throws Exception {
        stubFor(get(urlEqualTo("/unmodified"))
                .withHeader(IF_MODIFIED_SINCE, equalTo("Sat, 29 Oct 1994 19:43:31 GMT"))
                .willReturn(aResponse()
                        .withStatus(304)
                        .withHeader(LAST_UPDATE, "Sat, 29 Oct 1994 19:43:31 GMT")
                        .withHeader(CONTENT_LENGTH, "0")
                        .withBody("")));
        Request build = new Request.Builder()
                .get()
                .url(HTTP_LOCALHOST_8089 + "/unmodified")
                .addHeader(IF_MODIFIED_SINCE, "Sat, 29 Oct 1994 19:43:31 GMT")
                .build();
        Response res = client.newCall(build).execute();

        assertEquals(304, res.code());
        assertEquals(false, res.isSuccessful());
        assertEquals("0", res.header(CONTENT_LENGTH));
        assertEquals("Sat, 29 Oct 1994 19:43:31 GMT", res.header(LAST_UPDATE));
        assertEquals("", res.body().string());
    }

    @Test
    public void testError() throws Exception {
        stubFor(get(urlEqualTo("/error"))
                .willReturn(aResponse()
                        .withStatus(400)
                        .withHeader(CONTENT_LENGTH, "0")
                        .withBody("")));
        Request build = new Request.Builder()
                .get()
                .url(HTTP_LOCALHOST_8089 + "/error")
                .build();
        Response res = client.newCall(build).execute();

        assertEquals(400, res.code());
        assertEquals(false, res.isSuccessful());
        assertEquals("0", res.header(CONTENT_LENGTH));
        assertEquals("", res.body().string());
    }
}

@@ -688,33 +688,8 @@
 y;%0A%0A
-%0Aimport org.junit.Test;%0A%0A
 impo
@@ -761,16 +761,39 @@
 esponse;
+%0Aimport org.junit.Test;
 %0A%0Aimport
@@ -1186,16 +1186,59 @@
 tEquals;
+%0Aimport static org.junit.Assert.assertTrue;
 %0A%0Apublic
@@ -3633,11 +3633,332 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testConnectTanRabadHttps() throws Exception %7B%0A        Request build = new Request.Builder()%0A                .get()%0A                .url(%22https://www.tanrabad.org%22)%0A                .build();%0A%0A        Response res = client.newCall(build).execute();%0A%0A        assertTrue(res.isSuccessful());%0A    %7D
 %0A%7D%0A

test ใช้ Okhttp connect ไปที่ Index ของ TanRabad แบบ Https (ต้องต่อเน็ตถึงจะ test ผ่านนะแจ๊ะ) มันก็ผ่านนี้หน่า ทำไมก่อนหน้านี้ลองแล้วไม่ผ่านหว่า