<commit_before>package org.nutz.mvc.testapp;

import static org.junit.Assert.*;

import java.net.URL;
import java.util.Map;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.junit.After;
import org.junit.Before;
import org.nutz.http.Http;
import org.nutz.http.Request;
import org.nutz.http.Request.METHOD;
import org.nutz.http.Response;
import org.nutz.http.Sender;

/**
 * 需要Jetty 7.3.1 的jar包
 * 
 * @author wendal
 * 
 */
public abstract class BaseWebappTest {

    protected Server server;

    protected Response resp;

    @Before
    public void startServer() throws Throwable {

        try {
            URL url = getClass().getClassLoader().getResource("org/nutz/mvc/testapp/Root/FLAG");
            String path = url.toExternalForm();
            System.err.println(url);
            server = new Server(8888);
            String warUrlString = path.substring(0, path.length() - 4);
            server.setHandler(new WebAppContext(warUrlString, getContextPath()));
            server.start();
        }
        catch (Throwable e) {
            if (server != null)
                server.stop();
            throw e;
        }
    }

    @After
    public void shutdownServer() throws Throwable {
        if (server != null)
            server.stop();
    }

    public String getContextPath() {
        return "/nutztest";
    }

    public String getBaseURL() {
        return "http://localhost:8888" + getContextPath();
    }

    public Response get(String path) {
        resp = Http.get(getBaseURL() + path);
        assertNotNull(resp);
        return resp;
    }

    public Response post(String path, Map<String, Object> params) {
        resp = Sender.create(Request.create(getBaseURL() + path, METHOD.POST, params, null)).send();
        assertNotNull(resp);
        return resp;
    }

    public Response post(String path, String data) {
        Request req = Request.create(getBaseURL() + path, METHOD.POST);
        req.setData(data);
        resp = Sender.create(req).send();
        assertNotNull(resp);
        return resp;
    }

    public Response post(String path, byte[] bytes) {<commit_msg>通过junit跟maven test的测试<commit_after>package org.nutz.mvc.testapp;

import static org.junit.Assert.*;

import java.net.URL;
import java.util.Map;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.junit.After;
import org.junit.Before;
import org.nutz.http.Http;
import org.nutz.http.Request;
import org.nutz.http.Request.METHOD;
import org.nutz.http.Response;
import org.nutz.http.Sender;

/**
 * 需要Jetty 7.3.1 的jar包
 * 
 * @author wendal
 * 
 */
public abstract class BaseWebappTest {

    protected Server server;

    protected Response resp;

    private boolean isRunInMaven = false;

    private String serverURL = "http://localhost:8888";

    {
        for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
            if (ste.getClassName().startsWith("org.apache.maven.surefire")) {
                isRunInMaven = true;
                serverURL = "http://nutztest.herokuapp.com";
                break;
            }
        }
    }

    @Before
    public void startServer() throws Throwable {
        if (!isRunInMaven) {
            try {
                URL url = getClass().getClassLoader().getResource("org/nutz/mvc/testapp/Root/FLAG");
                String path = url.toExternalForm();
                System.err.println(url);
                server = new Server(8888);
                String warUrlString = path.substring(0, path.length() - 4);
                server.setHandler(new WebAppContext(warUrlString, getContextPath()));
                server.start();
            }
            catch (Throwable e) {
                if (server != null)
                    server.stop();
                throw e;
            }
        }
    }

    @After
    public void shutdownServer() throws Throwable {
        if (!isRunInMaven) {
            if (server != null)
                server.stop();
        }
    }

    public String getContextPath() {
        return "/nutztest";
    }

    public String getBaseURL() {
        return serverURL + getContextPath();
    }

    public Response get(String path) {
        resp = Http.get(getBaseURL() + path);
        assertNotNull(resp);
        return resp;
    }

    public Response post(String path, Map<String, Object> params) {
        resp = Sender.create(Request.create(getBaseURL() + path, METHOD.POST, params, null)).send();
        assertNotNull(resp);
        return resp;
    }

    public Response post(String path, String data) {
        Request req = Request.create(getBaseURL() + path, METHOD.POST);
        req.setData(data);
        resp = Sender.create(req).send();
        assertNotNull(resp);
        return resp;
    }

    public Response post(String path, byte[] bytes) {
{'fraction_non_alphanumeric': 0.11805555555555555, 'fraction_numerical': 0.0053104575163398695, 'mean_word_length': 2.2069417157825804, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6604932', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1410, 'n_words': 318}