Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Use HTTP Global Expected Codes #3

Merged
merged 2 commits into from

2 participants

@dadoonet

This PR uses global expected HTTP status codes when no specific status code is defined on a method.

{
    "name": "Amazon S3",
    "base_url": "http://s3.amazonaws.com",
    "version": "0.1",
    "methods": {
        "get_service": {
            "path": "/",
            "method": "GET",
            "headers": {
                "Date": "AWS"
            }
        },
        "delete_bucket": {
            "path": "/",
            "method": "DELETE",
            "headers": {
                "Date": "AWS"
            },
            "optional_params": [
                "bucket"
            ],
            "expected_status": [ 204 ]
        }
    },
    "expected_status": [ 200 ]
}

For example, the expected HTTP status code for get_service will be 200.

@nicoo
Owner

Thanks, I'll try and merge as soon as possible.

@nicoo nicoo merged commit 8f14c2d into nicoo:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
13 src/main/java/net/linkfluence/jspore/Model.java
@@ -3,9 +3,10 @@
package net.linkfluence.jspore;
import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableSet;
+
import java.net.URL;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
import java.util.Map.Entry;
/**
@@ -17,11 +18,13 @@
public URL baseUrl;
public String version;
public String name;
-
+
+ public final Collection<Integer> expectedStatus;
public final Map<String, Method> routes;
public Model() {
routes = new HashMap<String, Method>();
+ expectedStatus = new HashSet<Integer>();
}
public void addMethod(Method method){
@@ -55,7 +58,9 @@ public void merge(Model m){
if(!Strings.isNullOrEmpty(name)){
this.name = m.name;
}
-
+
+ this.expectedStatus.addAll(m.expectedStatus);
+
for(Entry<String, Method> e : m.routes.entrySet()){
this.routes.put(e.getKey(), e.getValue());
}
View
7 src/main/java/net/linkfluence/jspore/SpecParser.java
@@ -76,7 +76,8 @@ private Model parse(JsonNode root) throws InvalidSporeSpecException, MalformedUR
m.version = root.get(VERSION).getTextValue();
m.baseUrl = new URL(root.get(BASE_URL).getTextValue());
m.name = root.get(NAME).getTextValue();
-
+ m.expectedStatus.addAll(readIntegerCollection(root.get(EXPECTED_STATUS)));
+
// read all object properties to find method
JsonNode methods = root.get(METHODS);
Iterator<Entry<String, JsonNode>> nodeIt = methods.getFields();
@@ -89,6 +90,10 @@ private Model parse(JsonNode root) throws InvalidSporeSpecException, MalformedUR
String path = n.get(PATH).getTextValue();
Collection<String> requiredParams = readStringCollection(n.get(REQUIRED_PARAMS));
Collection<Integer> expectedStatuses = readIntegerCollection(n.get(EXPECTED_STATUS));
+ // If no status is defined on a method level, we use the default model expected statuses
+ if (expectedStatuses.isEmpty()) {
+ expectedStatuses.addAll(m.expectedStatus);
+ }
Collection<String> optionalParams = readStringCollection(n.get(OPTIONAL_PARAMS));
Map<String, String> headers = readStringMap(n.get(HEADERS));
boolean authentication = false;
View
4 src/main/java/net/linkfluence/jspore/Spore.java
@@ -105,7 +105,9 @@ private Method getMethod(String methodName) throws SporeException {
// response checking
int statusCode = res.getStatusCode();
if(!context.expectedStatus.isEmpty() && !context.expectedStatus.contains(statusCode)){
- throw new SporeException("Receive unexpected response status: " + statusCode);
+ String errorContent = res.getResponseBody();
+ throw new SporeException("Receive unexpected response status: " + statusCode
+ + ", message: " + errorContent);
}
if(this.responseMiddleware != null){
View
34 src/test/java/net/linkfluence/jspore/GitHubPrivateTest.java
@@ -30,33 +30,37 @@
private static Logger logger = LoggerFactory.getLogger(GitHubPrivateTest.class);
private static final String USERNAME = "nicoo";
- private static final String PASS = "xxxxxx";
+ private static final String PASS = "YOUHAVETOCHANGETHISPASSWORD";
- private final Spore<JsonNode> spore;
-
- public GitHubPrivateTest() throws FileNotFoundException, IOException, InvalidSporeSpecException {
+ private Spore<JsonNode> buildSpore(String login, String password) throws IOException, InvalidSporeSpecException {
URL url = this.getClass().getResource("/github.json");
File spec = new File(url.getFile());
- spore = new Spore.Builder<JsonNode>()
+ return new Spore.Builder<JsonNode>()
.addMiddleware(Spore.JSON)
- .addMiddleware(new Basic(USERNAME, PASS))
+ .addMiddleware(new Basic(login, password))
.addSpecContent(spec)
.setDebug(true)
.build();
}
@Test
- public void test_get_profile() throws SporeException {
- SporeResult<JsonNode> result = spore.call("get_profile",
- new ImmutableMap.Builder<String, String>()
- .build());
- JsonNode message = result.body.get("message");
- if (message != null) {
- assertEquals("Bad credentials", message.asText());
- logger.warn("You did not set USERNAME and PASS in GitHubPrivateTest class. So Github answers 'Bad credentials'.");
- } else {
+ public void test_get_profile() throws Exception {
+ SporeResult<JsonNode> result = null;
+ try {
+ result = buildSpore(USERNAME, PASS).call("get_profile",
+ new ImmutableMap.Builder<String, String>()
+ .build());
assertEquals("User", result.body.get("type").asText());
+ } catch (SporeException e) {
+ logger.warn("You did not set USERNAME and PASS in GitHubPrivateTest class. So Github answers 'Bad credentials'.", e);
}
}
+
+ @Test(expected = SporeException.class)
+ public void test_bad_credentials() throws Exception {
+ buildSpore(USERNAME, "BADPASSWORD").call("get_profile",
+ new ImmutableMap.Builder<String, String>()
+ .build());
+ }
}
View
50 src/test/java/net/linkfluence/jspore/SpecParserTest.java
@@ -1,7 +1,6 @@
package net.linkfluence.jspore;
-import java.io.File;
-import java.io.IOException;
+import java.io.*;
import java.net.URL;
import org.codehaus.jackson.JsonNode;
import org.junit.Test;
@@ -15,32 +14,7 @@
@Test
public void testParseSporeString() throws IOException, InvalidSporeSpecException {
- String spec = "{ "
- + "\"name\" : \"Amazon S3\", "
- + "\"version\" : \"0.1\", "
- + "\"base_url\" : \"http://s3.amazonaws.com\", "
- + "\"methods\" : { "
- + " \"get_service\" : { "
- + " \"path\" : \"/\", "
- + " \"method\" : \"GET\", "
- + " \"headers\" : { "
- + " \"Date\" : \"AWS\" "
- + " } "
- + " }, "
- + " \"delete_bucket\" : {"
- + " \"path\" : \"/\","
- + " \"method\" : \"DELETE\","
- + " \"headers\" : {"
- + " \"Date\" : \"AWS\""
- + " },"
- + " \"optional_params\" : ["
- + " \"bucket\""
- + " ],"
- + " \"expected_status\" : [ 204 ]"
- + " }"
- + "}"
- + "}"
- + "}";
+ String spec = readFromClasspath("/amazons3.json");
SpecParser parser = new SpecParser();
Model m = parser.parse(spec);
@@ -49,6 +23,7 @@ public void testParseSporeString() throws IOException, InvalidSporeSpecException
assertEquals("Amazon S3", m.name);
assertEquals("0.1", m.version);
assertEquals("http://s3.amazonaws.com", m.baseUrl.toString());
+ assertEquals(1, m.expectedStatus.size());
assertNotNull(m.getMethod("get_service"));
assertNotNull(m.getMethod("delete_bucket"));
@@ -56,10 +31,18 @@ public void testParseSporeString() throws IOException, InvalidSporeSpecException
Method getService = m.getMethod("get_service");
assertEquals("/", getService.path);
assertEquals("GET", getService.httpMethod);
+ assertEquals(1, getService.expectedStatus.size());
+ for (Integer status : getService.expectedStatus) {
+ assertEquals(200, status.intValue());
+ }
Method deleteBucket = m.getMethod("delete_bucket");
assertEquals("/", deleteBucket.path);
assertEquals("DELETE", deleteBucket.httpMethod);
+ assertEquals(1, deleteBucket.expectedStatus.size());
+ for (Integer status : deleteBucket.expectedStatus) {
+ assertEquals(204, status.intValue());
+ }
}
@@ -71,5 +54,14 @@ public void testLoadGitHubSpec() throws IOException, InvalidSporeSpecException{
.addSpecContent(spec)
.build();
}
-
+
+ protected static String readFromClasspath(String url) {
+ InputStream in = SpecParserTest.class.getResourceAsStream(url);
+ return convertStreamToString(in);
+ }
+
+ private static String convertStreamToString(java.io.InputStream is) {
+ java.util.Scanner s = new java.util.Scanner(is, "UTF-8").useDelimiter("\\A");
+ return s.hasNext() ? s.next() : "";
+ }
}
View
26 src/test/resources/amazons3.json
@@ -0,0 +1,26 @@
+{
+ "name": "Amazon S3",
+ "base_url": "http://s3.amazonaws.com",
+ "version": "0.1",
+ "methods": {
+ "get_service": {
+ "path": "/",
+ "method": "GET",
+ "headers": {
+ "Date": "AWS"
+ }
+ },
+ "delete_bucket": {
+ "path": "/",
+ "method": "DELETE",
+ "headers": {
+ "Date": "AWS"
+ },
+ "optional_params": [
+ "bucket"
+ ],
+ "expected_status": [ 204 ]
+ }
+ },
+ "expected_status": [ 200 ]
+}
Something went wrong with that request. Please try again.