package com.intuit.karate.core;

import static com.intuit.karate.TestUtils.*;
import static com.intuit.karate.TestUtils.runScenario;
import com.intuit.karate.http.HttpServer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author pthomas3
 */
class KarateHttpMockHandlerTest {

    static final Logger logger = LoggerFactory.getLogger(KarateHttpMockHandlerTest.class);

    MockHandler handler;
    HttpServer server;
    FeatureBuilder mock;
    ScenarioRuntime runtime;    
    
    String urlStep() {
        return "url 'http://localhost:" + server.getPort() + "'";
    }

    void startMockServer() {
        handler = new MockHandler(mock.build());
        server = new HttpServer(0, handler);       
    }

    FeatureBuilder background(String... lines) {
        mock = FeatureBuilder.background(lines);
        return mock;
    }

    Object get(String name) {
        return runtime.engine.vars.get(name).getValue();
    }

    ScenarioRuntime run(String... lines) {        
        runtime = runScenario(null, lines);
        return runtime;
    }

    private void matchVar(String name, Object expected) {
        match(get(name), expected);
    }
    
    private void matchVarContains(String name, Object expected) {
        matchContains(get(name), expected);
    }    

    @AfterEach
    void afterEach() {
        server.stop();
    }

    @Test
    void testSimpleGet() {
        background().scenario(
                "pathMatches('/hello')",
                "def response = 'hello world'");
        startMockServer();
        run(
                urlStep(), 
                "path '/hello'",
                "method get"
        );
        matchVar("response", "hello world");
    }
    
    @Test
    void testThatCookieIsPartOfRequestForApache() {
        background().scenario(
                "pathMatches('/hello')",
                "def response = requestHeaders");
        startMockServer();
        run(
                urlStep(), 
                "path '/hello'",
                "cookie foo = 'bar'",
                "method get"
        );
        matchVarContains("response", "{ cookie: ['foo=bar'] }");        
        
    }

}

@@ -560,25 +560,17 @@
 runtime;
-    %0A    
+%0A
 %0A    Str
@@ -780,23 +780,16 @@
 andler);
-       
 %0A    %7D%0A%0A
@@ -1050,24 +1050,16 @@
 lines) %7B
-        
 %0A       
@@ -1222,29 +1222,25 @@
 ted);%0A    %7D%0A
-    
 %0A
+
     private 
@@ -1342,20 +1342,16 @@
 );%0A    %7D
-    
 %0A%0A    @A
@@ -1628,33 +1628,32 @@
       urlStep(),
- 
 %0A               
@@ -1761,21 +1761,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     @Tes
@@ -2012,17 +2012,16 @@
 lStep(),
- 
 %0A       
@@ -2188,20 +2188,23 @@
 r'%5D %7D%22);
+%0A
     
+%7D%0A
     %0A   
@@ -2208,18 +2208,554 @@
     
-    %0A    %7D
+@Test%0A    void testThatExoticContentTypeIsPreserved() %7B%0A        background().scenario(%0A                %22pathMatches('/hello')%22,%0A                %22def response = requestHeaders%22);%0A        startMockServer();%0A        run(%0A                urlStep(),%0A                %22path '/hello'%22,%0A                %22header Content-Type = 'application/xxx.pingixxxxxx.checkUsernamePassword+json'%22,%0A                %22method post%22%0A        );%0A        matchVarContains(%22response%22, %22%7B 'content-type': %5B'application/xxx.pingixxxxxx.checkUsernamePassword+json'%5D %7D%22);%0A    %7D    
 %0A%0A%7D%0A

test case to ensure karate does not mangle content-type header value