-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
validate.exs
executable file
·97 lines (72 loc) · 2.88 KB
/
validate.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env elixir
Mix.install([
{:req, "~> 0.5.0"}
])
ExUnit.start()
defmodule ValidationTests do
use ExUnit.Case, async: true
@server "http://whenwhere.nerves-project.org/"
test "head request w/o nonce" do
result = Req.head!(@server)
assert result.status == 200
assert result.headers["cache-control"] == ["no-cache, must-revalidate, max-age=0"]
assert result.headers["x-nonce"] == nil
[now] = result.headers["x-now"]
assert {:ok, _, _} = DateTime.from_iso8601(now)
end
test "head request w/ nonce" do
result = Req.head!("#{@server}?nonce=0123456789abcdef")
assert result.status == 200
assert result.headers["cache-control"] == ["no-cache, must-revalidate, max-age=0"]
assert result.headers["x-nonce"] == ["0123456789abcdef"]
end
test "invalid nonce" do
result = Req.head!("#{@server}?nonce=0123456789012345678901234567890123")
assert result.status == 400
end
test "get request w/o nonce" do
result = Req.get!(@server)
assert result.status == 200
assert result.headers["cache-control"] == ["no-cache, must-revalidate, max-age=0"]
assert result.headers["content-type"] == ["application/json"]
assert result.headers["x-nonce"] == nil
[now] = result.headers["x-now"]
assert {:ok, _, _} = DateTime.from_iso8601(now)
assert result.body["now"] == now
assert Map.has_key?(result.body, "time_zone")
assert Map.has_key?(result.body, "latitude")
assert Map.has_key?(result.body, "longitude")
# assert Map.has_key?(result.body, "city")
# assert Map.has_key?(result.body, "country_region")
assert Map.has_key?(result.body, "country")
assert Map.has_key?(result.body, "address")
end
test "time passes" do
result1 = Req.head!(@server)
result2 = Req.head!(@server)
[first_time_str] = result1.headers["x-now"]
[second_time_str] = result2.headers["x-now"]
first_time = NaiveDateTime.from_iso8601!(first_time_str)
second_time = NaiveDateTime.from_iso8601!(second_time_str)
delta = NaiveDateTime.diff(second_time, first_time, :millisecond)
assert delta > 0
assert delta < 1000
end
test "get request using Erlang terms" do
result = Req.get!(@server, headers: %{content_type: "application/x-erlang-binary"})
assert result.status == 200
assert result.headers["cache-control"] == ["no-cache, must-revalidate, max-age=0"]
assert result.headers["content-type"] == ["application/x-erlang-binary"]
assert result.headers["x-nonce"] == nil
[now] = result.headers["x-now"]
assert {:ok, _, _} = DateTime.from_iso8601(now)
body = result.body |> :erlang.binary_to_term([:safe])
assert is_map(body)
assert body["now"] == now
assert Map.has_key?(body, "time_zone")
assert Map.has_key?(body, "latitude")
assert Map.has_key?(body, "longitude")
assert Map.has_key?(body, "country")
assert Map.has_key?(body, "address")
end
end