/**
 * The MIT License
 *
 * Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package BehaviorTests;

import kong.unirest.JsonNode;
import kong.unirest.MockCallback;
import kong.unirest.Unirest;
import kong.unirest.UnirestConfigException;
import kong.unirest.json.JSONArray;
import kong.unirest.json.JSONObject;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;

import static kong.unirest.TestUtil.assertException;

public class UniBodyPostingTest extends BddTest {
    @Test
    public void testDefaults_String(){
        Unirest.post(MockServer.POST)
                .body("foo")
                .asObject(RequestCapture.class)
                .getBody()
                .assertContentType("text/plain; charset=UTF-8");
    }

    @Test
    public void canSetCharsetOfBody(){
        Unirest.post(MockServer.POST)
                .charset(StandardCharsets.US_ASCII)
                .body("foo")
                .asObject(RequestCapture.class)
                .getBody()
                .assertContentType("text/plain; charset=US-ASCII")
                .asserBody("foo")
                .assertCharset(StandardCharsets.US_ASCII);
    }

    @Test
    public void canSetCharsetOfBodyAfterMovingToBody(){
        Unirest.post(MockServer.POST)
                .body("foo")
                .charset(StandardCharsets.US_ASCII)
                .asObject(RequestCapture.class)
                .getBody()
                .assertContentType("text/plain; charset=US-ASCII")
                .asserBody("foo")
                .assertCharset(StandardCharsets.US_ASCII);
    }

    @Test
    public void testPostRawBody() {
        String sourceString = "'\"@こんにちは-test-123-" + Math.random();
        byte[] sentBytes = sourceString.getBytes();

        Unirest.post(MockServer.POST)
                .body(sentBytes)
                .asObject(RequestCapture.class)
                .getBody()
                .asserBody(sourceString);
    }

    @Test
    public void testAsyncCustomContentType() {
        Unirest.post(MockServer.POST)
                .header("accept", "application/json")
                .header("Content-Type", "application/json")
                .body("{\"hello\":\"world\"}")
                .asJsonAsync(new MockCallback<>(this, r -> parse(r)
                        .asserBody("{\"hello\":\"world\"}")
                        .assertHeader("Content-Type", "application/json"))
                );

        assertAsync();
    }


    @Test
    public void postAPojoObjectUsingTheMapper() {
        GetResponse postResponseMock = new GetResponse();
        postResponseMock.setUrl(MockServer.POST);

        Unirest.post(postResponseMock.getUrl())
                .header("accept", "application/json")
                .header("Content-Type", "application/json")
                .body(postResponseMock)
                .asObject(RequestCapture.class)
                .getBody()
                .asserBody("{\"url\":\"http://localhost:4567/post\"}");
    }

    @Test
    public void testDeleteBody() {
        String body = "{\"jsonString\":{\"members\":\"members1\"}}";
        Unirest.delete(MockServer.DELETE)
                .body(body)
                .asObject(RequestCapture.class)
                .getBody()
                .asserBody(body);
    }

    @Test
    public void postBodyAsJson() {
        JSONObject body = new JSONObject();
        body.put("krusty","krab");

        Unirest.post(MockServer.POST)
                .body(body)
                .asObject(RequestCapture.class)
                .getBody()
                .asserBody("{\"krusty\":\"krab\"}");
    }

    @Test
    public void postBodyAsJsonArray() {
        JSONArray body = new JSONArray();
        body.put(0, "krusty");
        body.put(1, "krab");

        Unirest.post(MockServer.POST)
                .body(body)
                .asObject(RequestCapture.class)
                .getBody()
                .asserBody("[\"krusty\",\"krab\"]");
    }

    @Test
    public void postBodyAsJsonNode() {
        JsonNode body = new JsonNode("{\"krusty\":\"krab\"}");

        Unirest.post(MockServer.POST)
                .body(body)
                .asObject(RequestCapture.class)
                .getBody()
                .asserBody("{\"krusty\":\"krab\"}");
    }

    @Test
    public void cantPostObjectWithoutObjectMapper(){
        Unirest.config().setObjectMapper(null);

        assertException(() -> Unirest.post(MockServer.POST).body(new Foo("die")),
                UnirestConfigException.class,
                "No Object Mapper Configured. Please config one with Unirest.config().setObjectMapper");
    }
}

@@ -1223,123 +1223,9 @@
 est.
-JsonNode;%0Aimport kong.unirest.MockCallback;%0Aimport kong.unirest.Unirest;%0Aimport kong.unirest.UnirestConfigException
+*
 ;%0Aim
@@ -1426,16 +1426,77 @@
 ception;
+%0Aimport static org.junit.jupiter.api.Assertions.assertEquals;
 %0A%0Apublic
@@ -5681,11 +5681,449 @@
 );%0A    %7D
+%0A%0A    @Test%0A    void theRequestBodyIsAString() %7B%0A        RequestBodyEntity request = Unirest.post(MockServer.POST)%0A                .basicAuth(%22foo%22, %22bar%22)%0A                .header(%22Content-Type%22, %22application/json%22)%0A                .queryString(%22foo%22, %22bar%22)%0A                .body(%22%7B%5C%22body%5C%22: %5C%22sample%5C%22%7D%22);%0A%0A        Object value = request.getBody().get().uniPart().getValue();%0A        assertEquals(%22%7B%5C%22body%5C%22: %5C%22sample%5C%22%7D%22, value);%0A    %7D
 %0A%7D%0A

test explaining issue #362