/*
 *    Copyright 2012 Jason Miller
 *
 * Licensed 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 jj.http.client;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import io.netty.channel.ChannelFuture;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.util.concurrent.GenericFutureListener;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

/**
 * @author jason
 *
 */
@RunWith(MockitoJUnitRunner.class)
public class HttpRequesterTest {
	
	@Mock HttpClient client;
	
	@Mock ChannelFuture future;
	
	@Spy EmbeddedChannel ch;
	
	@Captor ArgumentCaptor<GenericFutureListener<ChannelFuture>> listenerCaptor;
	
	@Captor ArgumentCaptor<HttpRequest> requestCaptor;
	
	@Test
	public void test() throws Exception {
		
		// given
		HttpRequester requester = new HttpRequester(client);
		given(client.connect(false, "foaas.com", 80)).willReturn(future);
		given(future.channel()).willReturn(ch);
		
		// when
		requester.requestTo("http://foaas.com/off/:to/:from")
			.param("to", "joe")
			.param("from", "iou")
			.param("me", "awesome")
			.get()
			.header(HttpHeaders.Names.ACCEPT, "application/json")
			.begin(new HttpResponseListener() {});
		
		// then
		verify(future).addListener(listenerCaptor.capture());
		
		// when
		listenerCaptor.getValue().operationComplete(future);
		
		// then
		verify(ch).writeAndFlush(requestCaptor.capture());
		
		HttpRequest request = requestCaptor.getValue();
		
		assertThat(request.uri(), is("/off/joe/iou?me=awesome"));
		assertThat(request.headers().get(HttpHeaders.Names.HOST), is("foaas.com"));
		assertThat(request.headers().get(HttpHeaders.Names.ACCEPT), is("application/json"));
	}

}

@@ -730,24 +730,76 @@
 DMockito.*;%0A
+%0Aimport java.util.concurrent.atomic.AtomicBoolean;%0A%0A
 import io.ne
@@ -875,16 +875,75 @@
 hannel;%0A
+import io.netty.handler.codec.http.DefaultLastHttpContent;%0A
 import i
@@ -1845,16 +1845,68 @@
 rn(ch);%0A
+%09%09final AtomicBoolean called = new AtomicBoolean();%0A
 %09%09%0A%09%09// 
@@ -2146,16 +2146,103 @@
 ener() %7B
+%0A%09%09%09%09@Override%0A%09%09%09%09protected void responseComplete() %7B%0A%09%09%09%09%09called.set(true);%0A%09%09%09%09%7D%0A%09%09%09
 %7D);%0A%09%09%0A%09
@@ -2630,24 +2630,24 @@
 aas.com%22));%0A
-
 %09%09assertThat
@@ -2721,14 +2721,135 @@
 son%22));%0A
+%09%09%0A%09%09// when%0A%09%09ch.pipeline().removeFirst();%0A%09%09ch.writeInbound(new DefaultLastHttpContent());%0A%09%09assertTrue(called.get());%0A
 %09%7D%0A%0A%7D%0A

test improvement