From e91160f292b10466dfa15e4f0b057f49de058058 Mon Sep 17 00:00:00 2001 From: Ujjwal Gupta Date: Wed, 4 Mar 2020 09:28:14 +0530 Subject: [PATCH] create body struct --- src/abstracts/controller.cr | 6 +- src/abstracts/guard.cr | 6 +- src/handlers/post_handler.cr | 6 +- src/model/body.cr | 57 +++++++++++++++++++ src/model/index.cr | 1 + .../e2e/controller/home_controller_spec.cr | 8 +-- .../e2e/controller/random_controller_spec.cr | 4 +- .../src/controllers/user_controller.cr | 4 +- tests/general/src/guards/user_validator.cr | 11 ++-- 9 files changed, 81 insertions(+), 22 deletions(-) create mode 100644 src/model/body.cr diff --git a/src/abstracts/controller.cr b/src/abstracts/controller.cr index c855b08..c7dc7e3 100644 --- a/src/abstracts/controller.cr +++ b/src/abstracts/controller.cr @@ -11,9 +11,9 @@ module Shivneri return @context.as(RequestHandler).route_match_info.params end - macro get_tuple_from_body(value) - {{value}}.get_tuple_from_hash_json_any.call(@context.as(RequestHandler).body) - end + # macro get_tuple_from_body(value) + # {{value}}.get_tuple_from_hash_json_any.call(@context.as(RequestHandler).body) + # end end end end diff --git a/src/abstracts/guard.cr b/src/abstracts/guard.cr index 23167e3..40df3e7 100644 --- a/src/abstracts/guard.cr +++ b/src/abstracts/guard.cr @@ -9,9 +9,9 @@ module Shivneri return @context.as(RequestHandler).route_match_info.params end - macro get_tuple_from_body(value) - {{value}}.get_tuple_from_hash_json_any.call(@context.as(RequestHandler).body) - end + # macro get_tuple_from_body(value) + # {{value}}.get_tuple_from_hash_json_any.call(@context.as(RequestHandler).body) + # end abstract def check(*args) end diff --git a/src/handlers/post_handler.cr b/src/handlers/post_handler.cr index c5828d3..dc51e14 100644 --- a/src/handlers/post_handler.cr +++ b/src/handlers/post_handler.cr @@ -5,7 +5,7 @@ module Shivneri class PostHandler < ControllerResultHandler getter body, file - @body = {} of String => JSON::Any + @body : Body = Body.new({} of String => JSON::Any) @file : FileManager = FileManager.new @@ -13,7 +13,7 @@ module Shivneri HTTP::FormData.parse(@request) do |part| case part.headers["Content-Type"] when MIME_TYPE["json"] - @body.merge!(JSON.parse(part.body).as_h) + @body.merge(JSON.parse(part.body).as_h) when MIME_TYPE["form_url_encoded"] HTTP::Params.parse(part.body.gets_to_end).each do |key, val| @body[key] = JSON::Any.new(val) @@ -44,7 +44,7 @@ module Shivneri else case contentType when MIME_TYPE["json"] - @body = JSON.parse(@request.body.as(IO)).as_h + @body.body_data = JSON.parse(@request.body.as(IO)).as_h when MIME_TYPE["form_url_encoded"] HTTP::Params.parse(@request.body.as(IO).gets_to_end).each do |key, val| @body[key] = JSON::Any.new(val) diff --git a/src/model/body.cr b/src/model/body.cr new file mode 100644 index 0000000..cb417a3 --- /dev/null +++ b/src/model/body.cr @@ -0,0 +1,57 @@ +require "json" + +module Shivneri + module MODEL + class Body + JSON.mapping( + body_data: Hash(String, JSON::Any) + ) + setter body_data + + @body_data : Hash(String, JSON::Any) + + def initialize(@body_data) + end + + def to_tuple(value) + return convert_to_tuple(value) + end + + macro convert_to_tuple(value) + return {{value}}.get_tuple_from_hash_json_any.call(@body_data) + end + + def as_hash + return @body_data + end + + def [](key : String) + return @body_data[key] + end + + def []=(key : String, value) + @body_data[key] = value + end + + def merge(hash) + @body_data.merge!(hash) + end + + def has_key(key : String) + @body_data.has_key?(key) + end + + def has_key?(key : String) + @body_data.has_key?(key) + end + + # def to_json + # return @body_data.to_json + # end + + # def to_json(value : JSON::Builder) + # return @body_data.to_json + # end + end + end +end diff --git a/src/model/index.cr b/src/model/index.cr index 2a0edc6..58a1ee0 100644 --- a/src/model/index.cr +++ b/src/model/index.cr @@ -9,3 +9,4 @@ require "./http_file" require "./view_engine_data" require "./view_read_option" require "./fort" +require "./body" diff --git a/tests/general/e2e/controller/home_controller_spec.cr b/tests/general/e2e/controller/home_controller_spec.cr index e1db49e..f82f13c 100644 --- a/tests/general/e2e/controller/home_controller_spec.cr +++ b/tests/general/e2e/controller/home_controller_spec.cr @@ -101,7 +101,7 @@ describe "HomeController" do "Content-Type" => "application/json", }, nil) response.status_code.should eq 200 - response.body.should eq "{}" + response.body.should eq "{\"body_data\":{}}" end it "/post with empty body" do @@ -110,7 +110,7 @@ describe "HomeController" do "Content-Type" => "application/json", }, ({} of String => String).to_json) response.status_code.should eq 200 - response.body.should eq "{}" + response.body.should eq "{\"body_data\":{}}" end it "/post with some body" do @@ -121,7 +121,7 @@ describe "HomeController" do key: "hello", }.to_json) response.status_code.should eq 200 - response.body.should eq "{\"key\":\"hello\"}" + response.body.should eq "{\"body_data\":{\"key\":\"hello\"}}" end it "/post with xml data" do @@ -131,7 +131,7 @@ describe "HomeController" do }, " hello") response.status_code.should eq 200 - response.body.should eq "{}" + response.body.should eq "{\"body_data\":{}}" end it "/post with http:get" do diff --git a/tests/general/e2e/controller/random_controller_spec.cr b/tests/general/e2e/controller/random_controller_spec.cr index 4299bd1..969308d 100644 --- a/tests/general/e2e/controller/random_controller_spec.cr +++ b/tests/general/e2e/controller/random_controller_spec.cr @@ -43,13 +43,13 @@ describe "UserController" do "content-type" => "application/x-www-form-urlencoded", }, HTTP::Params.encode({"hello" => "world"})) response.status_code.should eq 200 - response.body.should eq "{\"body\":{\"hello\":\"world\"},\"query\":{}}" + response.body.should eq "{\"body\":{\"body_data\":{\"hello\":\"world\"}},\"query\":{}}" end it "/form with get" do response = http_client.get("/form?" + HTTP::Params.encode({"hello" => "world"})) response.status_code.should eq 200 - response.body.should eq "{\"body\":{},\"query\":{\"hello\":\"world\"}}" + response.body.should eq "{\"body\":{\"body_data\":{}},\"query\":{\"hello\":\"world\"}}" end it "/error" do diff --git a/tests/general/src/controllers/user_controller.cr b/tests/general/src/controllers/user_controller.cr index d6d3e4c..fa7a5e2 100644 --- a/tests/general/src/controllers/user_controller.cr +++ b/tests/general/src/controllers/user_controller.cr @@ -1,5 +1,5 @@ module General - # @[Shields(AuthenticationShield)] + @[Shields(AuthenticationShield)] class UserController < Controller @[Worker("GET")] @[Route("/")] @@ -13,7 +13,7 @@ module General @[Guards(UserValidator)] # @[Inject(as_body(NamedTuple(name: String)))] def add_user - user = get_tuple_from_body(NamedTuple(id: Int32, name: String)) + user = body.to_tuple(NamedTuple(id: Int32, name: String)) # get_tuple_from_body(NamedTuple(id: Int32, name: String)) puts typeof(user) puts user.to_json return text_result("ok") diff --git a/tests/general/src/guards/user_validator.cr b/tests/general/src/guards/user_validator.cr index d0dca55..1c6c343 100644 --- a/tests/general/src/guards/user_validator.cr +++ b/tests/general/src/guards/user_validator.cr @@ -20,17 +20,18 @@ module General end def validate - user = get_tuple_from_body( - NamedTuple(id: Int32, name: String, password: String, gender: String, email: String, address: String) - ) + # user = get_tuple_from_body( + # NamedTuple(id: Int32, name: String, password: String, gender: String, email: String, address: String) + # ) + user = body.to_tuple(NamedTuple(id: Int32, name: String, password: String, gender: String, email: String, address: String)) if (user[:name].size < 5) return "name should be minimum 5 characters" elsif (user[:password].size < 5) return "password should be minimum 5 characters" elsif (["male", "female"].includes? user[:gender]) return "gender should be either male or female" - elsif (/\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.match user[:email] == nil) - return "email not valid" + # elsif (/\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.match user[:email] == nil) + # return "email not valid" elsif (user[:address].size < 10 || user[:address].size > 10) return "address length should be between 10 & 100" end