mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Added context specs
This commit is contained in:
		
							parent
							
								
									ad6baba12b
								
							
						
					
					
						commit
						5376eb8bb2
					
				
					 5 changed files with 36 additions and 27 deletions
				
			
		
							
								
								
									
										23
									
								
								spec/context_spec.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								spec/context_spec.cr
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,23 @@ | ||||||
|  | require "./spec_helper" | ||||||
|  | 
 | ||||||
|  | describe "Context" do | ||||||
|  |   it "has a default content type" do | ||||||
|  |     kemal = Kemal::Handler.new | ||||||
|  |     kemal.add_route "GET", "/" do |env| | ||||||
|  |       "Hello" | ||||||
|  |     end | ||||||
|  |     request = HTTP::Request.new("GET", "/") | ||||||
|  |     response = kemal.call(request) | ||||||
|  |     response.headers["Content-Type"].should eq("text/plain") | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   it "sets content type" do | ||||||
|  |     kemal = Kemal::Handler.new | ||||||
|  |     kemal.add_route "GET", "/" do |env| | ||||||
|  |       env.set_content_type "application/json" | ||||||
|  |     end | ||||||
|  |     request = HTTP::Request.new("GET", "/") | ||||||
|  |     response = kemal.call(request) | ||||||
|  |     response.headers["Content-Type"].should eq("application/json") | ||||||
|  |   end | ||||||
|  | end | ||||||
|  | @ -40,14 +40,4 @@ describe "Kemal::Handler" do | ||||||
|     response = kemal.call(request) |     response = kemal.call(request) | ||||||
|     response.body.should eq("hello world") |     response.body.should eq("hello world") | ||||||
|   end |   end | ||||||
| 
 |  | ||||||
|   it "sets content type" do |  | ||||||
|     kemal = Kemal::Handler.new |  | ||||||
|     kemal.add_route "GET", "/" do |env| |  | ||||||
|       env.response.content_type = "application/json" |  | ||||||
|     end |  | ||||||
|     request = HTTP::Request.new("GET", "/") |  | ||||||
|     response = kemal.call(request) |  | ||||||
|     response.headers["Content-Type"].should eq("application/json") |  | ||||||
|   end |  | ||||||
| end | end | ||||||
|  |  | ||||||
|  | @ -1,12 +1,11 @@ | ||||||
| require "./spec_helper" | require "./spec_helper" | ||||||
| 
 | 
 | ||||||
| describe "ParamParser" do | describe "ParamParser" do | ||||||
| 
 |  | ||||||
|   it "parses query params" do |   it "parses query params" do | ||||||
|     route = Route.new "POST", "/" do |env| |     route = Route.new "POST", "/" do |env| | ||||||
|       hasan = env.params["hasan"] |               hasan = env.params["hasan"] | ||||||
|       "Hello #{hasan}" |               "Hello #{hasan}" | ||||||
|     end |             end | ||||||
|     request = HTTP::Request.new("POST", "/?hasan=cemal") |     request = HTTP::Request.new("POST", "/?hasan=cemal") | ||||||
|     params = Kemal::ParamParser.new(route, request).parse |     params = Kemal::ParamParser.new(route, request).parse | ||||||
|     params["hasan"].should eq "cemal" |     params["hasan"].should eq "cemal" | ||||||
|  | @ -14,14 +13,13 @@ describe "ParamParser" do | ||||||
| 
 | 
 | ||||||
|   it "parses request body" do |   it "parses request body" do | ||||||
|     route = Route.new "POST", "/" do |env| |     route = Route.new "POST", "/" do |env| | ||||||
|       name = env.params["name"] |               name = env.params["name"] | ||||||
|       age = env.params["age"] |               age = env.params["age"] | ||||||
|       hasan = env.params["hasan"] |               hasan = env.params["hasan"] | ||||||
|       "Hello #{name} #{hasan} #{age}" |               "Hello #{name} #{hasan} #{age}" | ||||||
|     end |             end | ||||||
|     request = HTTP::Request.new("POST", "/?hasan=cemal", body: "name=serdar&age=99") |     request = HTTP::Request.new("POST", "/?hasan=cemal", body: "name=serdar&age=99") | ||||||
|     params = Kemal::ParamParser.new(route, request).parse |     params = Kemal::ParamParser.new(route, request).parse | ||||||
|     params.should eq({"hasan" => "cemal", "name" => "serdar", "age" => "99"}) |     params.should eq({"hasan" => "cemal", "name" => "serdar", "age" => "99"}) | ||||||
|   end |   end | ||||||
| 
 |  | ||||||
| end | end | ||||||
|  |  | ||||||
|  | @ -1,15 +1,13 @@ | ||||||
| class Kemal::Context | class Kemal::Context | ||||||
|   getter request |   getter request | ||||||
|   getter params |   getter params | ||||||
|  |   getter content_type | ||||||
| 
 | 
 | ||||||
|   def initialize(@request, @params) |   def initialize(@request, @params) | ||||||
|  |     @content_type = "text/plain" | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def response |   def set_content_type(content_type) | ||||||
|     @response ||= Response.new |     @content_type = content_type | ||||||
|   end |  | ||||||
| 
 |  | ||||||
|   def response? |  | ||||||
|     @response |  | ||||||
|   end |   end | ||||||
| end | end | ||||||
|  |  | ||||||
|  | @ -26,7 +26,7 @@ class Kemal::Handler < HTTP::Handler | ||||||
|         context = Context.new(request, params.not_nil!) |         context = Context.new(request, params.not_nil!) | ||||||
|         begin |         begin | ||||||
|           body = route.handler.call(context).to_s |           body = route.handler.call(context).to_s | ||||||
|           content_type = context.response?.try(&.content_type) || "text/plain" |           content_type = context.content_type | ||||||
|           return HTTP::Response.ok(content_type, body) |           return HTTP::Response.ok(content_type, body) | ||||||
|         rescue ex |         rescue ex | ||||||
|           return HTTP::Response.error("text/plain", ex.to_s) |           return HTTP::Response.error("text/plain", ex.to_s) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue