Skip to content

Commit

Permalink
Implement Response headers (sparckles#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
sansyrox authored Apr 2, 2022
1 parent cf8f743 commit 7d5db9d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ dist/
# DS_Store
*.DS_Store
.vscode/settings.json

# python venv
venv
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ socket2 = { version = "0.4.1", features = ["all"] }
actix = "0.12.0"
actix-web-actors = "4.0.0-beta.1"
uuid = { version = "0.8", features = ["serde", "v4"] }
serde = "1.0.136"
serde_json = "1.0.79"

[package.metadata.maturin]
name = "robyn"
14 changes: 1 addition & 13 deletions integration_tests/base_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,26 +166,14 @@ def shutdown_handler():

@app.get("/redirect")
async def redirect(request):
return {"status_code": "307", "body": "", "type": "text"}
return {"status_code": "307", "body": "", "type": "text", "headers": jsonify({"Location": "redirect_route"})}


@app.get("/redirect_route")
async def redirect_route(request):
return "This is the redirected route"


@app.before_request("/redirect")
async def redirect_before_request(request):
request["headers"]["Location"] = "redirect_route"
return ""


@app.after_request("/redirect")
async def redirect_after_request(request):
request["headers"]["Location"] = "redirect_route"
return ""


if __name__ == "__main__":
ROBYN_URL = os.getenv("ROBYN_URL", "0.0.0.0")
app.add_header("server", "robyn")
Expand Down
1 change: 1 addition & 0 deletions integration_tests/test_status_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def test_404_post_request_status_code(session):
r = requests.post(f"{BASE_URL}/404")
assert r.status_code == 404


def test_307_get_request(session):
r = requests.get(f"{BASE_URL}/redirect")
assert r.text == "This is the redirected route"
Expand Down
11 changes: 11 additions & 0 deletions src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::sync::Arc;

use actix_web::{http::Method, web, HttpRequest, HttpResponse, HttpResponseBuilder};
use anyhow::{bail, Result};
use serde_json::Value;
// pyO3 module
use crate::types::{Headers, PyFunction};
use futures_util::stream::StreamExt;
Expand Down Expand Up @@ -67,6 +68,16 @@ pub async fn handle_request(
let status_code =
actix_http::StatusCode::from_str(contents.get("status_code").unwrap()).unwrap();

let headers: HashMap<String, String> = match contents.get("headers") {
Some(headers) => {
let h: HashMap<String, String> = serde_json::from_str(headers).unwrap();
h
}
None => HashMap::new(),
};

println!("These are the headers from serde {:?}", headers);

let mut response = HttpResponse::build(status_code);
apply_headers(&mut response, headers.clone());
let final_response = if !body.is_empty() {
Expand Down

0 comments on commit 7d5db9d

Please sign in to comment.