Added tests, increased primitives version.
This commit is contained in:
		
							parent
							
								
									5f412ad55c
								
							
						
					
					
						commit
						98d379925e
					
				| 
						 | 
				
			
			@ -1,3 +1,3 @@
 | 
			
		|||
# data
 | 
			
		||||
# Handy-Http Data
 | 
			
		||||
 | 
			
		||||
Support for common data formats and database operations.
 | 
			
		||||
Support for common data formats and database operations.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										2
									
								
								dub.json
								
								
								
								
							
							
						
						
									
										2
									
								
								dub.json
								
								
								
								
							| 
						 | 
				
			
			@ -6,7 +6,7 @@
 | 
			
		|||
	"dependencies": {
 | 
			
		||||
		"asdf": "~>0.7",
 | 
			
		||||
		"dxml": "~>0.4",
 | 
			
		||||
		"handy-http-primitives": "~>1.4"
 | 
			
		||||
		"handy-http-primitives": "~>1.6"
 | 
			
		||||
	},
 | 
			
		||||
	"description": "Support for common data formats and database operations.",
 | 
			
		||||
	"license": "CC0",
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,10 +3,10 @@
 | 
			
		|||
	"versions": {
 | 
			
		||||
		"asdf": "0.7.17",
 | 
			
		||||
		"dxml": "0.4.4",
 | 
			
		||||
		"handy-http-primitives": "1.4.0",
 | 
			
		||||
		"handy-http-primitives": "1.6.0",
 | 
			
		||||
		"mir-algorithm": "3.22.3",
 | 
			
		||||
		"mir-core": "1.7.1",
 | 
			
		||||
		"silly": "1.1.1",
 | 
			
		||||
		"streams": "3.5.0"
 | 
			
		||||
		"streams": "3.6.0"
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,6 +26,19 @@ T readJsonBodyAs(T)(ref ServerHttpRequest request) {
 | 
			
		|||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
unittest {
 | 
			
		||||
    import handy_http_primitives.builder;
 | 
			
		||||
    ServerHttpRequest request = ServerHttpRequestBuilder()
 | 
			
		||||
        .withBody("{\"key\": \"value\"}")
 | 
			
		||||
        .withHeader("Content-Length", "16")
 | 
			
		||||
        .build();
 | 
			
		||||
    struct TestStruct {
 | 
			
		||||
        string key;
 | 
			
		||||
    }
 | 
			
		||||
    TestStruct testStruct = readJsonBodyAs!TestStruct(request);
 | 
			
		||||
    assert(testStruct.key == "value");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/** 
 | 
			
		||||
 * Writes a JSON value to the body of the given HTTP response, serializing it
 | 
			
		||||
 * using the ASDF library. Will also set Content-Type and Content-Length
 | 
			
		||||
| 
						 | 
				
			
			@ -57,9 +70,25 @@ void writeJsonBody(T)(ref ServerHttpResponse response, in T bodyContent) {
 | 
			
		|||
        StreamResult result = response.outputStream.writeToStream(cast(ubyte[]) responseBody);
 | 
			
		||||
        if (result.hasError) {
 | 
			
		||||
            StreamError err = result.error;
 | 
			
		||||
            throw new HttpStatusException(HttpStatus.INTERNAL_SERVER_ERROR, err.message);
 | 
			
		||||
            throw new HttpStatusException(HttpStatus.INTERNAL_SERVER_ERROR, cast(string) err.message);
 | 
			
		||||
        }
 | 
			
		||||
    } catch (SerdeException e) {
 | 
			
		||||
        throw new HttpStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.msg, e);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
unittest {
 | 
			
		||||
    import handy_http_primitives.builder;
 | 
			
		||||
    ArrayOutputStream!ubyte outputStream = byteArrayOutputStream();
 | 
			
		||||
    ServerHttpResponse response = ServerHttpResponseBuilder()
 | 
			
		||||
        .withOutputStream(&outputStream)
 | 
			
		||||
        .build();
 | 
			
		||||
    struct TestStruct {
 | 
			
		||||
        string key;
 | 
			
		||||
    }
 | 
			
		||||
    TestStruct testStruct = TestStruct("value");
 | 
			
		||||
    writeJsonBody(response, testStruct);
 | 
			
		||||
    assert(response.headers["Content-Type"] == "application/json");
 | 
			
		||||
    const writtenBody = cast(string) outputStream.toArrayRaw();
 | 
			
		||||
    assert(writtenBody == "{\"key\":\"value\"}");
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue