/*
 * Copyright 2011 the original author or authors.
 *
 * 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 co.freeside.betamax.message.httpclient;

import java.util.*;
import co.freeside.betamax.message.*;
import com.google.common.base.*;
import com.google.common.collect.*;
import org.apache.http.*;

public abstract class HttpMessageAdapter<T extends HttpMessage> extends AbstractMessage implements Message {
    protected abstract T getDelegate();

    @Override
    public final Map<String, String> getHeaders() {
        ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
        for (Header header : getDelegate().getAllHeaders()) {
            String headerName = header.getName();
            builder.put(headerName, getHeader(headerName));
        }
        return builder.build();
    }

    @Override
    public final String getHeader(String name) {
        List<Header> headers = Arrays.asList(getDelegate().getHeaders(name));
        List<String> headerValues = Lists.transform(headers, new Function<Header, String>() {
            @Override
            public String apply(Header header) {
                return header.getValue();
            }
        });
        return Joiner.on(", ").skipNulls().join(headerValues);
    }

    @Override
    public final void addHeader(String name, String value) {
        getDelegate().addHeader(name, value);
    }

}

@@ -711,24 +711,71 @@
 .message.*;%0A
+import co.freeside.betamax.util.MultimapUtils;%0A
 import com.g
@@ -1092,16 +1092,21 @@
 mutableM
+ultim
 ap.Build
@@ -1144,16 +1144,21 @@
 mutableM
+ultim
 ap.build
@@ -1241,102 +1241,54 @@
     
-String headerName = header.getName();%0A            builder.put(headerName, getHeader(headerName
+builder.put(header.getName(), header.getValue(
 ));%0A
@@ -1312,16 +1312,38 @@
  return 
+MultimapUtils.flatten(
 builder.
@@ -1349,16 +1349,23 @@
 .build()
+, %22, %22)
 ;%0A    %7D%0A

Fix duplicated headers