Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*******************************************************************************
* Copyright (c) 2013 Andreas Mihm.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Andreas Mihm - initial API and implementation
******************************************************************************/
package com.eclipsesource.restfuse.example;

import org.junit.Rule;
import org.junit.runner.RunWith;

import com.eclipsesource.restfuse.Destination;
import com.eclipsesource.restfuse.HttpJUnitRunner;
import com.eclipsesource.restfuse.Method;
import com.eclipsesource.restfuse.RequestContext;
import com.eclipsesource.restfuse.Response;
import com.eclipsesource.restfuse.annotation.Context;
import com.eclipsesource.restfuse.annotation.HttpTest;

/**
* CAUTION: The way how this class uses restfuse to be able to send dynamic
* request bodies is a workaround which conflicts a bit with some principles of
* junit usage. So its an example how to approach this problem, but the final
* solution in restfuse should be to provide a annotation free way to use
* restfuse.
*
* @author mihm
*
*/
@RunWith(HttpJUnitRunner.class)
public class DynamicBodyTest {

@Rule
public Destination restfuse = getDestination();

@Context
private Response response;

// static variale to store the requestBody to be sent
private static String requestBody;

@HttpTest(method = Method.GET, path = "/v1/items", order = 1)
public void a_getItems() {
// assertOk( response );
String jsonResponse = response.getBody();

// prepare next test method
requestBody = "{\"name\":\"testitem\", \"description\":\"a new item\"}";
}

@HttpTest(method = Method.POST, path = "/v1/items", order = 2)
public void b_addItem() {
// assertOk( response );
String jsonResponse = response.getBody();
}

private Destination getDestination() {
Destination destination = new Destination(this, "http://localhost");
RequestContext context = destination.getRequestContext();
if (requestBody != null) {
context.setDynamicBody(requestBody);
}
return destination;
}
}
706 changes: 353 additions & 353 deletions com.eclipsesource.restfuse/.settings/org.eclipse.jdt.core.prefs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions com.eclipsesource.restfuse/.settings/org.eclipse.jdt.ui.prefs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Fri Jul 29 11:29:44 CEST 2011
eclipse.preferences.version=1
formatter_profile=_pe
formatter_settings_version=12
#Fri Jul 29 11:29:44 CEST 2011
eclipse.preferences.version=1
formatter_profile=_pe
formatter_settings_version=12
2 changes: 1 addition & 1 deletion com.eclipsesource.restfuse/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: restfuse
Bundle-SymbolicName: com.eclipsesource.restfuse
Bundle-Version: 1.2.0.qualifier
Bundle-Version: 1.2.1.qualifier
Bundle-Vendor: EclipseSource
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Export-Package: com.eclipsesource.restfuse;version="1.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public enum MediaType {
APPLICATION_ATOM_XML( "application/atom+xml" ),
APPLICATION_XHTML_XML( "application/xhtml+xml" ),
APPLICATION_SVG_XML( "application/svg+xml" ),
APPLICATION_JSON( "application/json" ),
APPLICATION_JSON( "application/json;charset=utf-8" ),
APPLICATION_FORM_URLENCODED( "application/x-www-form-urlencoded" ),
MULTIPART_FORM_DATA( "multipart/form-data" ),
APPLICATION_OCTET_STREAM( "application/octet-stream" ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class RequestContext {

private Map<String, String> dynamicPathSegments = new HashMap<String, String>();

private String dynamicBody;

/**
* <p>
* Adds a header attribute to a request.
Expand Down Expand Up @@ -79,5 +81,24 @@ public Map<String, String> getHeaders() {
public Map<String, String> getPathSegments() {
return new HashMap<String, String>( dynamicPathSegments );
}


public String getDynamicBody() {
return dynamicBody;
}


/**
* added workaround enhancement for manipulating the body sent with the request in a basic dynamic way
*
* @author mihm
*
* @param dynamicBody
*/
public void setDynamicBody( String dynamicBody ) {
this.dynamicBody = dynamicBody;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,26 @@ public class InternalRequest {
private final String url;
private InputStream content;
private String mediaType;

private String contentString;

public InternalRequest( String url ) {
this.url = url;
headers = new HashMap<String, List<String>>();
authentications = new ArrayList<AuthenticationInfo>();
}


public String getContentString() {
return contentString;
}

public void setContentString( String contentString ) {
this.contentString = contentString;
}



public void setContentType( String mediaType ) {
this.mediaType = mediaType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public InternalRequest createRequest( RequestContext context ) {
addAuthentication( call, request );
addContentType( call, request );
addHeader( call, request, context );
addBody( call, request );
addBody( call, request, context );
return request;
}

Expand Down Expand Up @@ -110,11 +110,23 @@ private void addHeadersFromAnnotation( HttpTest call, InternalRequest request )
}
}

private void addBody( HttpTest test, InternalRequest request ) {
/**
* @author Andreas Mihm added the check fo a dynamic request body which will be send with the request
*
* @param test
* @param request
* @param context
*/
private void addBody( HttpTest test, InternalRequest request, RequestContext context ) {
if( !test.file().equals( "" ) ) {
request.setContentString( test.file() );
request.setContent( getFileStream( test.file() ) );
} else if( !test.content().equals( "" ) ) {
request.setContentString( test.content() );
request.setContent( getContentStream( test.content() ) );
} else if (context.getDynamicBody() != null && !context.getDynamicBody().equals( "" )) {
request.setContentString( context.getDynamicBody() );
request.setContent( getContentStream( context.getDynamicBody() ) );
}
}

Expand Down