1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.http.test;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertTrue;
15
16 import java.io.File;
17 import java.io.IOException;
18 import java.net.URI;
19 import java.util.HashMap;
20 import java.util.List;
21
22 import org.eclipse.jetty.servlet.DefaultServlet;
23 import org.eclipse.jetty.servlet.ServletContextHandler;
24 import org.eclipse.jetty.servlet.ServletHolder;
25 import org.eclipse.jgit.junit.TestRepository;
26 import org.eclipse.jgit.junit.http.AccessEvent;
27 import org.eclipse.jgit.lib.Repository;
28 import org.eclipse.jgit.revwalk.RevBlob;
29 import org.eclipse.jgit.revwalk.RevCommit;
30 import org.eclipse.jgit.transport.HttpTransport;
31 import org.eclipse.jgit.transport.Transport;
32 import org.eclipse.jgit.transport.TransportHttp;
33 import org.eclipse.jgit.transport.URIish;
34 import org.eclipse.jgit.transport.http.HttpConnectionFactory;
35 import org.junit.Before;
36 import org.junit.Test;
37
38 public class SetAdditionalHeadersTest extends AllFactoriesHttpTestCase {
39
40 private URIish remoteURI;
41
42 private RevBlob A_txt;
43
44 private RevCommit A, B;
45
46 public SetAdditionalHeadersTest(HttpConnectionFactory cf) {
47 super(cf);
48 }
49
50 @Override
51 @Before
52 public void setUp() throws Exception {
53 super.setUp();
54
55 final TestRepository<Repository> src = createTestRepository();
56 final File srcGit = src.getRepository().getDirectory();
57 final URI base = srcGit.getParentFile().toURI();
58
59 ServletContextHandler app = server.addContext("/git");
60 app.setResourceBase(base.toString());
61 ServletHolder holder = app.addServlet(DefaultServlet.class, "/");
62
63 holder.setInitParameter("aliases", "true");
64 server.setUp();
65
66 remoteURI = toURIish(app, srcGit.getName());
67
68 A_txt = src.blob("A");
69 A = src.commit().add("A_txt", A_txt).create();
70 B = src.commit().parent(A).add("A_txt", "C").add("B", "B").create();
71 src.update(master, B);
72 }
73
74 @Test
75 public void testSetHeaders() throws IOException {
76 Repository dst = createBareRepository();
77
78 assertEquals("http", remoteURI.getScheme());
79
80 try (Transport t = Transport.open(dst, remoteURI)) {
81 assertTrue("isa TransportHttp", t instanceof TransportHttp);
82 assertTrue("isa HttpTransport", t instanceof HttpTransport);
83
84 HashMap<String, String> headers = new HashMap<>();
85 headers.put("Cookie", "someTokenValue=23gBog34");
86 headers.put("AnotherKey", "someValue");
87 ((TransportHttp) t).setAdditionalHeaders(headers);
88 t.openFetch();
89 }
90
91 List<AccessEvent> requests = getRequests();
92 assertEquals(2, requests.size());
93
94 AccessEvent info = requests.get(0);
95 assertEquals("GET", info.getMethod());
96 assertEquals(info.getRequestHeader("Cookie"), "someTokenValue=23gBog34");
97 assertEquals(info.getRequestHeader("AnotherKey"), "someValue");
98 assertEquals(200, info.getStatus());
99
100 info = requests.get(1);
101 assertEquals("GET", info.getMethod());
102 assertEquals(info.getRequestHeader("Cookie"), "someTokenValue=23gBog34");
103 assertEquals(info.getRequestHeader("AnotherKey"), "someValue");
104 assertEquals(200, info.getStatus());
105 }
106
107 }