CatFilterTest.java 3.8 KB
Newer Older
1 2 3 4
package com.dianping.cat.servlet;

import java.io.IOException;
import java.io.InputStream;
5
import java.io.PrintWriter;
6
import java.util.EnumSet;
7 8 9
import java.util.HashMap;
import java.util.List;
import java.util.Map;
10 11
import java.util.concurrent.TimeUnit;

12
import javax.servlet.DispatcherType;
13 14 15 16 17
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

18
import org.eclipse.jetty.webapp.WebAppContext;
19
import org.junit.Test;
20 21
import org.unidal.helper.Files;
import org.unidal.helper.Joiners;
22
import org.unidal.helper.Urls;
23
import org.unidal.test.jetty.JettyServer;
24 25 26 27

import com.dianping.cat.Cat;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;
28

29 30
import junit.framework.Assert;

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
public class CatFilterTest extends JettyServer {
	@Override
	protected String getContextPath() {
		return "/mock";
	}

	@Override
	protected int getServerPort() {
		return 2282;
	}

	@Override
	protected boolean isWebXmlDefined() {
		return false;
	}

	@Override
	protected void postConfigure(WebAppContext context) {
		context.addServlet(MockServlet.class, "/*");
50
		context.addFilter(CatFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
51 52
	}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	@Test
	public void testMode0() throws Exception {
		String url = "http://localhost:2282/mock/mode0";
		InputStream in = Urls.forIO().openStream(url);
		String content = Files.forIO().readFrom(in, "utf-8");

		Assert.assertEquals("mock content here!", content);

		TimeUnit.MILLISECONDS.sleep(100);
	}

	@Test
	public void testMode1() throws Exception {
		String url = "http://localhost:2282/mock/mode1";
		Transaction t = Cat.newTransaction("Mock", "testMode1");
68 69 70 71 72 73 74

		try {
			String childId = Cat.createMessageId();
			String id = Cat.getManager().getThreadLocalMessageTree().getMessageId();

			Cat.logEvent("RemoteCall", url, Message.SUCCESS, childId);

75
			InputStream in = Urls.forIO().connectTimeout(100) //
76 77 78 79
			      .header("X-Cat-Id", childId) //
			      .header("X-Cat-Parent-Id", id) //
			      .header("X-Cat-Root-Id", id) //
			      .openStream(url);
80
			String content = Files.forIO().readFrom(in, "utf-8");
81

82 83 84
			Assert.assertEquals("mock content here!", content);

			t.setStatus(Message.SUCCESS);
85 86 87 88 89 90
		} finally {
			t.complete();
		}

		TimeUnit.MILLISECONDS.sleep(100);
	}
91 92 93 94 95 96 97

	@Test
	public void testMode2() throws Exception {
		String url = "http://localhost:2282/mock/mode2";
		Map<String, List<String>> headers = new HashMap<String, List<String>>();
		InputStream in = Urls.forIO().connectTimeout(100) //
		      .header("X-Cat-Source", "container") //
Y
fix bug  
youyong205 已提交
98
		      .header("X-CAT-TRACE-MODE", "true") //
99 100 101 102 103 104
		      .openStream(url, headers);
		String content = Files.forIO().readFrom(in, "utf-8");

		Assert.assertEquals("mock content here!", content);

		String rootId = getHeader(headers, "X-CAT-ROOT-ID");
F
Frankie Wu 已提交
105
		String catServer = getHeader(headers, "X-CAT-SERVER");
106 107

		Assert.assertNotNull(rootId);
F
Frankie Wu 已提交
108
		Assert.assertNotNull(catServer);
109 110 111 112 113 114 115

		TimeUnit.MILLISECONDS.sleep(100);
	}

	private String getHeader(Map<String, List<String>> headers, String name) {
		List<String> values = headers.get(name);

Y
fix bug  
youyong205 已提交
116 117 118 119 120 121 122 123 124 125
		if (values != null) {
			int len = values.size();

			if (len == 0) {
				return null;
			} else if (len == 1) {
				return values.get(0);
			} else {
				return Joiners.by(',').join(values);
			}
F
Frankie Wu 已提交
126
		} else {
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
			return null;
		}
	}

	public static class MockServlet extends HttpServlet {
		private static final long serialVersionUID = 1L;

		@Override
		protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
			PrintWriter writer = res.getWriter();
			Transaction t = Cat.newTransaction("Mock", req.getRequestURI());

			try {
				writer.write("mock content here!");

				// no status set by purpose
			} finally {
				t.complete();
			}
		}
	}
148
}