Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
hexisdylan committed Jul 24, 2022
1 parent a0b409a commit 920895f
Show file tree
Hide file tree
Showing 11 changed files with 376 additions and 69 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,41 @@
16. pragstudio-elixir-15-enum-2 - [Enum 2](https://github.com/skedaddl3/elixir-pragstudio/commit/2136779039cb6ab6eb5bc06424e5e7c7dffcb1f0)

17. pragstudio-elixir-16-comprehensions - [Comprehensions](https://github.com/skedaddl3/elixir-pragstudio/commit/e0ddef222ccd1f5fc53adb5cd8b712a3897ce327)

18. pragstudio-elixir-17-phoenix - [Phoenix](https://github.com/skedaddl3/phoenix-framework-pragstudio) Redirects to another repository

19. pragstudio-elixir-18-testing - [Testing]()

20. pragstudio-elixir-19-external-library - [External Library]()

21. pragstudio-elixir-20-web-sockets - [Web Sockets]()

22. pragstudio-elixir-21-processes-1 - [Processes 1]()

23. pragstudio-elixir-21-processes-2 - [Processes 2]()

24. pragstudio-elixir-22-messages - [Messages]()

25. pragstudio-elixir-23-tasks - [Tasks]()

26. pragstudio-elixir-24-stateful-server-1 - [Stateful Server 1]()

27. pragstudio-elixir-24-stateful-server-2 - [Stateful Server 2]()

28. pragstudio-elixir-24-stateful-server-3 - [Stateful Server 3]()

29. pragstudio-elixir-24-stateful-server-4 - [Stateful Server 4]()

30. pragstudio-elixir-25-refactor-server - [Refactor Server]()

31. pragstudio-elixir-26-gen-server - [Gen Server]()

32. pragstudio-elixir-27-another-genserver - [Another Gen Server]()

33. pragstudio-elixir-28-linking-processes - [Linking Processes]()

34. pragstudio-elixir-29-supervisors-1 - [Supervisors 1]()

35. pragstudio-elixir-29-supervisors-2 - [Supervisors 2]()

36. pragstudio-elixir-30-application - [Application]()
2 changes: 1 addition & 1 deletion lib/bear_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ defmodule Servy.BearController do
end

def create(conv, params) do
%{conv | status: 201, resp_body: "Created a #{params["type"]} bear named #{params["name"]}"}
%{conv | status: 201, resp_body: "Created a #{params["type"]} bear named #{params["name"]}!"}
end
end
110 changes: 55 additions & 55 deletions lib/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,80 +84,80 @@ defmodule Servy.Handler do

def format_response(%Conv{} = conv) do
"""
HTTP/1.1 #{Conv.full_status(conv)}
Content-Type: text/html
Content-Length: #{String.length(conv.resp_body)}
HTTP/1.1 #{Conv.full_status(conv)}\r
Content-Type: text/html\r
Content-Length: #{String.length(conv.resp_body)}\r
\r
#{conv.resp_body}
"""
end
end

request = """
GET /bigfoot HTTP/1.1
Host: example.com
User-Agent: ExampleBrowser/1.0
Accept: */*
# request = """
# GET /bigfoot HTTP/1.1
# Host: example.com
# User-Agent: ExampleBrowser/1.0
# Accept: */*

"""
# """

response = Servy.Handler.handle(request)
IO.puts(response)
# response = Servy.Handler.handle(request)
# IO.puts(response)

request = """
GET /wildthings HTTP/1.1
Host: example.com
User-Agent: ExampleBrowser/1.0
Accept: */*
# request = """
# GET /wildthings HTTP/1.1
# Host: example.com
# User-Agent: ExampleBrowser/1.0
# Accept: */*

"""
# """

response = Servy.Handler.handle(request)
IO.puts(response)
# response = Servy.Handler.handle(request)
# IO.puts(response)

request = """
GET /bears HTTP/1.1
Host: example.com
User-Agent: ExampleBrowser/1.0
Accept: */*
# request = """
# GET /bears HTTP/1.1
# Host: example.com
# User-Agent: ExampleBrowser/1.0
# Accept: */*

"""
# """

response = Servy.Handler.handle(request)
IO.puts(response)
# response = Servy.Handler.handle(request)
# IO.puts(response)

request = """
GET /bears/1 HTTP/1.1
Host: example.com
User-Agent: ExampleBrowser/1.0
Accept: */*
# request = """
# GET /bears/1 HTTP/1.1
# Host: example.com
# User-Agent: ExampleBrowser/1.0
# Accept: */*

"""
# """

response = Servy.Handler.handle(request)
IO.puts(response)
# response = Servy.Handler.handle(request)
# IO.puts(response)

request = """
GET /about HTTP/1.1
Host: example.com
User-Agent: ExampleBrowser/1.0
Accept: */*
# request = """
# GET /about HTTP/1.1
# Host: example.com
# User-Agent: ExampleBrowser/1.0
# Accept: */*

"""
# """

response = Servy.Handler.handle(request)
IO.puts(response)
# response = Servy.Handler.handle(request)
# IO.puts(response)

request = """
POST /bears HTTP/1.1
Host: example.com
User-Agent: ExampleBrowser/1.0
Accept: */*
Content-Type: application/x-www-form-urlencoded
Content-Length: 21
# request = """
# POST /bears HTTP/1.1
# Host: example.com
# User-Agent: ExampleBrowser/1.0
# Accept: */*
# Content-Type: application/x-www-form-urlencoded
# Content-Length: 21

name=Baloo&type=Brown
"""
# name=Baloo&type=Brown
# """

response = Servy.Handler.handle(request)
IO.puts(response)
# response = Servy.Handler.handle(request)
# IO.puts(response)
19 changes: 16 additions & 3 deletions lib/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ defmodule Servy.Parser do
alias Servy.Conv

def parse(request) do
[top, params_string] = String.split(request, "\n\n")
[top, params_string] = String.split(request, "\r\n\r\n")

[request_line | header_lines] = String.split(top, "\n")
[request_line | header_lines] = String.split(top, "\r\n")

[method, path, _] = String.split(request_line, " ")

Expand All @@ -28,13 +28,26 @@ defmodule Servy.Parser do

# IO.puts("Key: #{inspect(key)} Value: #{inspect(value)}")

headers = Map.put(%{}, key, value)
# headers = Map.put(%{}, key, value)
# IO.inspect(headers)
parse_headers(tail, headers)
end

def parse_headers([], headers), do: headers

@doc """
Parses the given param string of the form `key1=value1&key2=value2`
into a map with corresponding keys and values.
## Examples
iex> params_string = "name=Baloo&type=Brown"
iex> Servy.Parser.parse_params("application/x-www-form-urlencoded", params_string)
%{"name" => "Baloo", "type" => "Brown"}
iex> Servy.Parser.parse_params("multipart/form-data", params_string)
%{}
"""

def parse_params("application/x-www-form-urlencoded", params_string) do
params_string |> String.trim() |> URI.decode_query()
end
Expand Down
13 changes: 11 additions & 2 deletions lib/plugins.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ defmodule Servy.Plugins do
alias Servy.Conv, as: Conv
@doc "Logs 404 requests"
def track(%{status: 404, path: path} = conv) do
IO.puts("Warning: #{path} is on the loose!")
if Mix.env() != :test do
IO.puts("Warning: #{path} is on the loose!")
end

conv
end

Expand All @@ -14,5 +17,11 @@ defmodule Servy.Plugins do

def rewrite_path(%Conv{} = conv), do: conv

def log(%Conv{} = conv), do: IO.inspect(conv)
def log(%Conv{} = conv) do
if Mix.env() == :dev do
IO.inspect(conv)
end

conv
end
end
7 changes: 3 additions & 4 deletions pages/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ <h1>Clark's Wildthings Refuge</h1>

<blockquote>
When we contemplate the whole globe as one great dewdrop, striped and dotted
with continents and islands, flying through space and dotted with continents
and islands, flying through space with other stars all singing and shining
together as one, the whole universe appears as an infinite storm of beauty. --
John Muir
with continents and islands, flying through space with other stars all singing
and shining together as one, the whole universe appears as an infinite storm
of beauty. -- John Muir
</blockquote>
2 changes: 1 addition & 1 deletion templates/index.eex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h1>All the Bears!</h1>
<h1>All The Bears!</h1>

<ul>
<%= for bear <- bears do %>
Expand Down
3 changes: 2 additions & 1 deletion templates/show.eex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<h1>Show Bear</h1>

<p>
Is <%= bear.name %> Hibernating? <strong><%= bear.hibernating %></strong>
Is <%= bear.name %> hibernating? <strong><%= bear.hibernating %></strong>
</p>
Loading

0 comments on commit 920895f

Please sign in to comment.