Search in sources :

Example 26 with Header

use of org.apache.http.Header in project gerrit by GerritCodeReview.

the class CorsIT method check.

private RestResponse check(String url, boolean accept, String origin) throws Exception {
    Header hdr = new BasicHeader(ORIGIN, origin);
    RestResponse r = adminRestSession.getWithHeader(url, hdr);
    r.assertOK();
    checkCors(r, accept, origin);
    return r;
}
Also used : Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) RestResponse(com.google.gerrit.acceptance.RestResponse) BasicHeader(org.apache.http.message.BasicHeader)

Example 27 with Header

use of org.apache.http.Header in project gerrit by GerritCodeReview.

the class ChangeRebuilderIT method rebuildEntitiesCreatedByImpersonation.

@Test
public void rebuildEntitiesCreatedByImpersonation() throws Exception {
    PushOneCommit.Result r = createChange();
    Change.Id id = r.getPatchSetId().getParentKey();
    PatchSet.Id psId = new PatchSet.Id(id, 1);
    String prefix = "/changes/" + id + "/revisions/current/";
    // For each of the entities that have a real user field, create one entity
    // without impersonation and one with.
    CommentInput ci = new CommentInput();
    ci.path = Patch.COMMIT_MSG;
    ci.side = Side.REVISION;
    ci.line = 1;
    ci.message = "comment without impersonation";
    ReviewInput ri = new ReviewInput();
    ri.label("Code-Review", -1);
    ri.message = "message without impersonation";
    ri.drafts = DraftHandling.KEEP;
    ri.comments = ImmutableMap.of(ci.path, ImmutableList.of(ci));
    userRestSession.post(prefix + "review", ri).assertOK();
    DraftInput di = new DraftInput();
    di.path = Patch.COMMIT_MSG;
    di.side = Side.REVISION;
    di.line = 1;
    di.message = "draft without impersonation";
    userRestSession.put(prefix + "drafts", di).assertCreated();
    allowRunAs();
    try {
        Header runAs = new BasicHeader("X-Gerrit-RunAs", user.id.toString());
        ci.message = "comment with impersonation";
        ri.message = "message with impersonation";
        ri.label("Code-Review", 1);
        adminRestSession.postWithHeader(prefix + "review", ri, runAs).assertOK();
        di.message = "draft with impersonation";
        adminRestSession.putWithHeader(prefix + "drafts", runAs, di).assertCreated();
    } finally {
        removeRunAs();
    }
    List<ChangeMessage> msgs = Ordering.natural().onResultOf(ChangeMessage::getWrittenOn).sortedCopy(db.changeMessages().byChange(id));
    assertThat(msgs).hasSize(3);
    assertThat(msgs.get(1).getMessage()).endsWith("message without impersonation");
    assertThat(msgs.get(1).getAuthor()).isEqualTo(user.id);
    assertThat(msgs.get(1).getRealAuthor()).isEqualTo(user.id);
    assertThat(msgs.get(2).getMessage()).endsWith("message with impersonation");
    assertThat(msgs.get(2).getAuthor()).isEqualTo(user.id);
    assertThat(msgs.get(2).getRealAuthor()).isEqualTo(admin.id);
    List<PatchSetApproval> psas = db.patchSetApprovals().byChange(id).toList();
    assertThat(psas).hasSize(1);
    assertThat(psas.get(0).getLabel()).isEqualTo("Code-Review");
    assertThat(psas.get(0).getValue()).isEqualTo(1);
    assertThat(psas.get(0).getAccountId()).isEqualTo(user.id);
    assertThat(psas.get(0).getRealAccountId()).isEqualTo(admin.id);
    Ordering<PatchLineComment> commentOrder = Ordering.natural().onResultOf(PatchLineComment::getWrittenOn);
    List<PatchLineComment> drafts = commentOrder.sortedCopy(db.patchComments().draftByPatchSetAuthor(psId, user.id));
    assertThat(drafts).hasSize(2);
    assertThat(drafts.get(0).getMessage()).isEqualTo("draft without impersonation");
    assertThat(drafts.get(0).getAuthor()).isEqualTo(user.id);
    assertThat(drafts.get(0).getRealAuthor()).isEqualTo(user.id);
    assertThat(drafts.get(1).getMessage()).isEqualTo("draft with impersonation");
    assertThat(drafts.get(1).getAuthor()).isEqualTo(user.id);
    assertThat(drafts.get(1).getRealAuthor()).isEqualTo(admin.id);
    List<PatchLineComment> pub = commentOrder.sortedCopy(db.patchComments().publishedByPatchSet(psId));
    assertThat(pub).hasSize(2);
    assertThat(pub.get(0).getMessage()).isEqualTo("comment without impersonation");
    assertThat(pub.get(0).getAuthor()).isEqualTo(user.id);
    assertThat(pub.get(0).getRealAuthor()).isEqualTo(user.id);
    assertThat(pub.get(1).getMessage()).isEqualTo("comment with impersonation");
    assertThat(pub.get(1).getAuthor()).isEqualTo(user.id);
    assertThat(pub.get(1).getRealAuthor()).isEqualTo(admin.id);
}
Also used : PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) PatchLineComment(com.google.gerrit.reviewdb.client.PatchLineComment) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) CommentInput(com.google.gerrit.extensions.api.changes.ReviewInput.CommentInput) ChangeMessage(com.google.gerrit.reviewdb.client.ChangeMessage) DraftInput(com.google.gerrit.extensions.api.changes.DraftInput) ObjectId(org.eclipse.jgit.lib.ObjectId) BasicHeader(org.apache.http.message.BasicHeader) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 28 with Header

use of org.apache.http.Header in project lucene-solr by apache.

the class NoCacheHeaderTest method doCacheControl.

@Override
protected void doCacheControl(String method) throws Exception {
    HttpRequestBase m = getSelectMethod(method);
    HttpResponse response = getClient().execute(m);
    checkResponseBody(method, response);
    Header head = response.getFirstHeader("Cache-Control");
    assertNull("We got a cache-control header in response", head);
    head = response.getFirstHeader("Expires");
    assertNull("We got an Expires header in response", head);
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse)

Example 29 with Header

use of org.apache.http.Header in project lucene-solr by apache.

the class ResponseHeaderTest method testHttpResponse.

@Test
public void testHttpResponse() throws SolrServerException, IOException {
    HttpSolrClient client = (HttpSolrClient) getSolrClient();
    HttpClient httpClient = client.getHttpClient();
    URI uri = URI.create(client.getBaseURL() + "/withHeaders?q=*:*");
    HttpGet httpGet = new HttpGet(uri);
    HttpResponse response = httpClient.execute(httpGet);
    Header[] headers = response.getAllHeaders();
    boolean containsWarningHeader = false;
    for (Header header : headers) {
        if ("Warning".equals(header.getName())) {
            containsWarningHeader = true;
            assertEquals("This is a test warning", header.getValue());
            break;
        }
    }
    assertTrue("Expected header not found", containsWarningHeader);
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) Header(org.apache.http.Header) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) Test(org.junit.Test)

Example 30 with Header

use of org.apache.http.Header in project lucene-solr by apache.

the class CacheHeaderTest method doLastModified.

@Override
protected void doLastModified(String method) throws Exception {
    // We do a first request to get the last modified
    // This must result in a 200 OK response
    HttpRequestBase get = getSelectMethod(method);
    HttpResponse response = getClient().execute(get);
    checkResponseBody(method, response);
    assertEquals("Got no response code 200 in initial request", 200, response.getStatusLine().getStatusCode());
    Header head = response.getFirstHeader("Last-Modified");
    assertNotNull("We got no Last-Modified header", head);
    Date lastModified = DateUtils.parseDate(head.getValue());
    // If-Modified-Since tests
    get = getSelectMethod(method);
    get.addHeader("If-Modified-Since", DateUtils.formatDate(new Date()));
    response = getClient().execute(get);
    checkResponseBody(method, response);
    assertEquals("Expected 304 NotModified response with current date", 304, response.getStatusLine().getStatusCode());
    get = getSelectMethod(method);
    get.addHeader("If-Modified-Since", DateUtils.formatDate(new Date(lastModified.getTime() - 10000)));
    response = getClient().execute(get);
    checkResponseBody(method, response);
    assertEquals("Expected 200 OK response with If-Modified-Since in the past", 200, response.getStatusLine().getStatusCode());
    // If-Unmodified-Since tests
    get = getSelectMethod(method);
    get.addHeader("If-Unmodified-Since", DateUtils.formatDate(new Date(lastModified.getTime() - 10000)));
    response = getClient().execute(get);
    checkResponseBody(method, response);
    assertEquals("Expected 412 Precondition failed with If-Unmodified-Since in the past", 412, response.getStatusLine().getStatusCode());
    get = getSelectMethod(method);
    get.addHeader("If-Unmodified-Since", DateUtils.formatDate(new Date()));
    response = getClient().execute(get);
    checkResponseBody(method, response);
    assertEquals("Expected 200 OK response with If-Unmodified-Since and current date", 200, response.getStatusLine().getStatusCode());
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse) Date(java.util.Date)

Aggregations

Header (org.apache.http.Header)906 HttpResponse (org.apache.http.HttpResponse)368 HttpGet (org.apache.http.client.methods.HttpGet)253 Test (org.junit.Test)206 IOException (java.io.IOException)200 BasicHeader (org.apache.http.message.BasicHeader)160 HttpEntity (org.apache.http.HttpEntity)134 TestHttpClient (io.undertow.testutils.TestHttpClient)94 URI (java.net.URI)93 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)93 ArrayList (java.util.ArrayList)90 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)78 HashMap (java.util.HashMap)76 InputStream (java.io.InputStream)67 URISyntaxException (java.net.URISyntaxException)64 HttpPost (org.apache.http.client.methods.HttpPost)62 StatusLine (org.apache.http.StatusLine)60 List (java.util.List)52 StringEntity (org.apache.http.entity.StringEntity)51 HttpHost (org.apache.http.HttpHost)46