-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be02a67
commit eda7833
Showing
3 changed files
with
48 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,37 @@ | ||
defmodule Loggex do | ||
require IEx | ||
use Plug.Router | ||
use Plug.Builder | ||
|
||
plug Plug.Parsers, parsers: [:json, :urlencoded], | ||
json_decoder: JSX | ||
plug :timefixer | ||
plug :match | ||
plug :dispatch | ||
|
||
def start do | ||
Plug.Adapters.Cowboy.http Loggex, [], port: 6438 | ||
Loggex.Repo.start_link | ||
end | ||
|
||
def stop do | ||
Plug.Adapters.Cowboy.shutdown Loggex.HTTP | ||
end | ||
|
||
post "/log" do | ||
Map.keys(conn.params) | ||
|> Enum.reduce(%{}, fn(k,acc) -> Map.put(acc, String.to_atom(k), conn.params[k]) end) | ||
|> (&(Map.merge(%Loggex.Logline{}, &1))).() | ||
|> Loggex.Repo.insert | ||
send_resp(conn, 200, "No Response") | ||
end | ||
|
||
def timefixer conn, opts do | ||
conn = conn.params["sendtime"] | ||
|> Ecto.DateTime.cast! | ||
|> (&(Map.put(conn.params, "sendtime", &1))).() | ||
|> (&(Map.put(conn, :params, &1))).() | ||
conn | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
defmodule LoglineTest do | ||
use ExUnit.Case | ||
|
||
test "schema can be inserted and retrieved" do | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,37 @@ | ||
defmodule LoggexTest do | ||
use ExUnit.Case | ||
import Ecto.Query | ||
doctest Loggex | ||
|
||
#test the start method of loggex | ||
#start should start the server | ||
|
||
setup do | ||
Loggex.start | ||
:random.seed(:erlang.now) | ||
{:ok, repo} = Loggex.start | ||
on_exit fn -> | ||
Loggex.stop | ||
end | ||
:ok | ||
end | ||
|
||
test "route exists" do | ||
{:ok, response} = HTTPoison.post "localhost:6438/log", "", [] | ||
assert response.status_code == 200 | ||
test "route exists" do | ||
{:ok, response} = HTTPoison.post "localhost:6438/log", JSX.encode!(%{"sendtime" => Ecto.DateTime.to_iso8601(Ecto.DateTime.utc)}), %{"content-type" => "application/json"} | ||
assert response.status_code == 200 | ||
end | ||
|
||
test "route inserts a properly formatted logline into the database" do | ||
code = Enum.random(200..600) | ||
body = "\{\"body\": \"content\"\}" | ||
time = Ecto.DateTime.utc |> Ecto.DateTime.to_iso8601 | ||
{:ok, response} = HTTPoison.post "localhost:6438/log", | ||
JSX.encode!(%{sender: "http://example.com", | ||
sendtime: time, | ||
responseCode: code, | ||
body: body}), | ||
%{"Content-Type" => "application/json"} | ||
|
||
query = from l in Loggex.Logline, | ||
where: l.responseCode == ^code, | ||
select: l | ||
logline = Loggex.Repo.one query | ||
assert logline.body == body | ||
end | ||
end |