/*
* Copyright 2011 Daniel Josefsson
*
* 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 se.dannej.fakehttpserver.expect;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import se.dannej.fakehttpserver.FakeHttpServletRequest;
import se.dannej.fakehttpserver.FakeHttpServletResponse;
import se.dannej.fakehttpserver.FakeServerException;
/**
* Convenience class for creating {@link ResponseAction}s.
*/
public class ResponseActions {
/**
* Set status code
* @param status
* @return
*/
public static ResponseAction sendStatus(final int status) {
return new ResponseAction() {
@Override
public void applyTo(FakeHttpServletRequest request, FakeHttpServletResponse response) {
response.setStatus(status);
}
};
}
/**
* Set content-type
* @param contentType
* @return
*/
public static ResponseAction sendContentType(final String contentType) {
return new ResponseAction() {
@Override
public void applyTo(FakeHttpServletRequest request, FakeHttpServletResponse response) {
response.setContentType(contentType);
}
};
}
/**
* Set content
* @param content
* @return
*/
public static ResponseAction sendContent(final String content) {
return new ResponseAction() {
@Override
public void applyTo(FakeHttpServletRequest request, FakeHttpServletResponse response) {
try {
IOUtils.write(content, response.getOutputStream());
} catch (IOException e) {
throw new FakeServerException("Failed writing content to response", e);
}
}
};
}
public static ResponseAction sendHeader(final String headerName, final String headerValue) {
return new ResponseAction() {
@Override
public void applyTo(FakeHttpServletRequest request, FakeHttpServletResponse response) {
response.addHeader(headerName, headerValue);
}
};
}
}