17a68ed6a49c3060b235810391a82412a95f9c979jwilson/* 27a68ed6a49c3060b235810391a82412a95f9c979jwilson * Copyright (C) 2012 Google Inc. 37a68ed6a49c3060b235810391a82412a95f9c979jwilson * 47a68ed6a49c3060b235810391a82412a95f9c979jwilson * Licensed under the Apache License, Version 2.0 (the "License"); 57a68ed6a49c3060b235810391a82412a95f9c979jwilson * you may not use this file except in compliance with the License. 67a68ed6a49c3060b235810391a82412a95f9c979jwilson * You may obtain a copy of the License at 77a68ed6a49c3060b235810391a82412a95f9c979jwilson * 87a68ed6a49c3060b235810391a82412a95f9c979jwilson * http://www.apache.org/licenses/LICENSE-2.0 97a68ed6a49c3060b235810391a82412a95f9c979jwilson * 107a68ed6a49c3060b235810391a82412a95f9c979jwilson * Unless required by applicable law or agreed to in writing, software 117a68ed6a49c3060b235810391a82412a95f9c979jwilson * distributed under the License is distributed on an "AS IS" BASIS, 127a68ed6a49c3060b235810391a82412a95f9c979jwilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 137a68ed6a49c3060b235810391a82412a95f9c979jwilson * See the License for the specific language governing permissions and 147a68ed6a49c3060b235810391a82412a95f9c979jwilson * limitations under the License. 157a68ed6a49c3060b235810391a82412a95f9c979jwilson */ 167a68ed6a49c3060b235810391a82412a95f9c979jwilsonpackage com.google.mockwebserver; 177a68ed6a49c3060b235810391a82412a95f9c979jwilson 187a68ed6a49c3060b235810391a82412a95f9c979jwilsonimport java.net.HttpURLConnection; 197a68ed6a49c3060b235810391a82412a95f9c979jwilsonimport java.util.concurrent.BlockingQueue; 207a68ed6a49c3060b235810391a82412a95f9c979jwilsonimport java.util.concurrent.LinkedBlockingQueue; 217a68ed6a49c3060b235810391a82412a95f9c979jwilson 227a68ed6a49c3060b235810391a82412a95f9c979jwilson/** 237a68ed6a49c3060b235810391a82412a95f9c979jwilson * Default dispatcher that processes a script of responses. Populate the script by calling 247a68ed6a49c3060b235810391a82412a95f9c979jwilson * {@link #enqueueResponse(MockResponse)}. 257a68ed6a49c3060b235810391a82412a95f9c979jwilson */ 267a68ed6a49c3060b235810391a82412a95f9c979jwilsonpublic class QueueDispatcher extends Dispatcher { 277a68ed6a49c3060b235810391a82412a95f9c979jwilson protected final BlockingQueue<MockResponse> responseQueue 287a68ed6a49c3060b235810391a82412a95f9c979jwilson = new LinkedBlockingQueue<MockResponse>(); 297a68ed6a49c3060b235810391a82412a95f9c979jwilson private MockResponse failFastResponse; 307a68ed6a49c3060b235810391a82412a95f9c979jwilson 317a68ed6a49c3060b235810391a82412a95f9c979jwilson @Override public MockResponse dispatch(RecordedRequest request) throws InterruptedException { 327a68ed6a49c3060b235810391a82412a95f9c979jwilson // to permit interactive/browser testing, ignore requests for favicons 337a68ed6a49c3060b235810391a82412a95f9c979jwilson final String requestLine = request.getRequestLine(); 347a68ed6a49c3060b235810391a82412a95f9c979jwilson if (requestLine != null && requestLine.equals("GET /favicon.ico HTTP/1.1")) { 357a68ed6a49c3060b235810391a82412a95f9c979jwilson System.out.println("served " + requestLine); 367a68ed6a49c3060b235810391a82412a95f9c979jwilson return new MockResponse() 377a68ed6a49c3060b235810391a82412a95f9c979jwilson .setResponseCode(HttpURLConnection.HTTP_NOT_FOUND); 387a68ed6a49c3060b235810391a82412a95f9c979jwilson } 397a68ed6a49c3060b235810391a82412a95f9c979jwilson 407a68ed6a49c3060b235810391a82412a95f9c979jwilson if (failFastResponse != null && responseQueue.peek() == null) { 417a68ed6a49c3060b235810391a82412a95f9c979jwilson // Fail fast if there's no response queued up. 427a68ed6a49c3060b235810391a82412a95f9c979jwilson return failFastResponse; 437a68ed6a49c3060b235810391a82412a95f9c979jwilson } 447a68ed6a49c3060b235810391a82412a95f9c979jwilson 457a68ed6a49c3060b235810391a82412a95f9c979jwilson return responseQueue.take(); 467a68ed6a49c3060b235810391a82412a95f9c979jwilson } 477a68ed6a49c3060b235810391a82412a95f9c979jwilson 48d5e25502a3ed333011753d5f2e1484072a7f5617Neil Fuller @Override public MockResponse peek() { 497a68ed6a49c3060b235810391a82412a95f9c979jwilson MockResponse peek = responseQueue.peek(); 50d5e25502a3ed333011753d5f2e1484072a7f5617Neil Fuller if (peek != null) return peek; 51d5e25502a3ed333011753d5f2e1484072a7f5617Neil Fuller if (failFastResponse != null) return failFastResponse; 52d5e25502a3ed333011753d5f2e1484072a7f5617Neil Fuller return super.peek(); 537a68ed6a49c3060b235810391a82412a95f9c979jwilson } 547a68ed6a49c3060b235810391a82412a95f9c979jwilson 557a68ed6a49c3060b235810391a82412a95f9c979jwilson public void enqueueResponse(MockResponse response) { 567a68ed6a49c3060b235810391a82412a95f9c979jwilson responseQueue.add(response); 577a68ed6a49c3060b235810391a82412a95f9c979jwilson } 587a68ed6a49c3060b235810391a82412a95f9c979jwilson 597a68ed6a49c3060b235810391a82412a95f9c979jwilson public void setFailFast(boolean failFast) { 607a68ed6a49c3060b235810391a82412a95f9c979jwilson MockResponse failFastResponse = failFast 617a68ed6a49c3060b235810391a82412a95f9c979jwilson ? new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND) 627a68ed6a49c3060b235810391a82412a95f9c979jwilson : null; 637a68ed6a49c3060b235810391a82412a95f9c979jwilson setFailFast(failFastResponse); 647a68ed6a49c3060b235810391a82412a95f9c979jwilson } 657a68ed6a49c3060b235810391a82412a95f9c979jwilson 667a68ed6a49c3060b235810391a82412a95f9c979jwilson public void setFailFast(MockResponse failFastResponse) { 677a68ed6a49c3060b235810391a82412a95f9c979jwilson this.failFastResponse = failFastResponse; 687a68ed6a49c3060b235810391a82412a95f9c979jwilson } 697a68ed6a49c3060b235810391a82412a95f9c979jwilson} 70