Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.shindig.gadgets.http;
 
 
 import  com.google.common.collect.Maps;
 import  com.google.inject.Provider;
 import static org.junit.Assert.assertEquals;
 import org.junit.Test;
 
 import java.util.Map;
 
 public class DefaultRequestPipelineTest {
   private static final Uri DEFAULT_URI = Uri.parse("http://example.org/gadget.xml");
 
   private final FakeHttpFetcher fetcher = new FakeHttpFetcher();
   private final FakeHttpCache cache = new FakeHttpCache();
   private final FakeOAuthRequestProvider oauth = new FakeOAuthRequestProvider();
 
       new NoOpImageRewriter(), new NoOpInvalidationService());
 
   @Test
   public void authTypeNoneNotCached() throws Exception {
     HttpRequest request = new HttpRequest()
         .setAuthType(.);
 
     . = new HttpResponse("response");
 
     HttpResponse response = .execute(request);
 
     assertEquals(request.);
     assertEquals(.response);
     assertEquals(response..get());
   }
 
   @Test
   public void authTypeNoneWasCached() throws Exception {
     HttpRequest request = new HttpRequest()
         .setAuthType(.);
 
     HttpResponse cached = new HttpResponse("cached");
     ..put(cached);
 
     HttpResponse response = .execute(request);
 
     assertEquals(cachedresponse);
   }
 
   @Test
   public void authTypeNoneWasCachedButStale() throws Exception {
     HttpRequest request = new HttpRequest()
         .setAuthType(.);
 
     HttpResponse cached = new HttpResponseBuilder().setStrictNoCache().create();
     ..put(cached);
 
     HttpResponse fetched = new HttpResponse("fetched");
     . = fetched;
 
     HttpResponse response = .execute(request);
 
     assertEquals(fetchedresponse);
     assertEquals(request.);
     assertEquals(fetched..get());
   }
 
   @Test
   public void authTypeNoneIgnoreCache() throws Exception {
     HttpRequest request = new HttpRequest()
        .setAuthType(.)
        .setIgnoreCache(true);
    HttpResponse fetched = new HttpResponse("fetched");
    . = fetched;
    HttpResponse response = .execute(request);
    assertEquals(fetchedresponse);
    assertEquals(request.);
  }
  @Test
  public void authTypeOAuthNotCached() throws Exception {
    HttpRequest request = new HttpRequest()
        .setAuthType(.);
    . = new HttpResponse("oauth result");
    HttpResponse response = .execute(request);
    assertEquals(.response);
    assertEquals(request.);
    assertEquals(response..get());
  }
  @Test
  public void authTypeOAuthWasCached() throws Exception {
    HttpRequest request = new HttpRequest()
        .setAuthType(.);
    HttpResponse cached = new HttpResponse("cached");
    ..put(cached);
    HttpResponse response = .execute(request);
    assertEquals(cachedresponse);
  }
  public static class FakeHttpFetcher implements HttpFetcher {
    protected HttpRequest request;
    protected HttpResponse response;
    protected int fetchCount = 0;
    protected FakeHttpFetcher() {
    }
    public HttpResponse fetch(HttpRequest requestthrows GadgetException {
      ++;
      this. = request;
      if ( == null) {
      }
      return ;
    }
  }
  public static class FakeHttpCache implements HttpCache {
    protected final Map<UriHttpResponsedata = Maps.newHashMap();
    protected int writeCount = 0;
    protected int readCount = 0;
    protected FakeHttpCache() {
    }
    public boolean addResponse(HttpRequest requestHttpResponse response) {
      ++;
      .put(request.getUri(), response);
      return true;
    }
    public HttpResponse getResponse(HttpRequest request) {
      ++;
      return .get(request.getUri());
    }
    public HttpResponse removeResponse(HttpRequest key) {
      throw new UnsupportedOperationException();
    }
    public String createKey(HttpRequest request) {
      return request.getUri().getQuery();
    }
  }
  public static class FakeOAuthRequestProvider implements Provider<OAuthRequest> {
    protected int fetchCount = 0;
    protected HttpRequest httpRequest;
    protected HttpResponse httpResponse;
    protected FakeOAuthRequestProvider() {
    }
    private final OAuthRequest oauthRequest = new OAuthRequest(nullnull) {
      @Override
      public HttpResponse fetch(HttpRequest request) {
        ++;
         = request;
        return ;
      }
    };
    public OAuthRequest get() {
      return ;
    }
  }
New to GrepCode? Check out our FAQ X