v2 / vlib / net / http / http_test.v
102 lines · 94 sloc · 2.32 KB · de8226d3d2a6485a46b68dfa4d835403052b13c4
Raw
1import net.http
2
3fn test_https_get() {
4 $if !network ? {
5 return
6 }
7 assert http.get_text('https://vlang.io/version') == '0.1.5'
8 println('https ok')
9}
10
11fn test_http_get_from_vlang_utc_now() {
12 $if !network ? {
13 return
14 }
15 url := 'http://vlang.io/utc_now'
16 println('Test getting current time from HTTP ${url} by http.get')
17 res := http.get(url) or { panic(err) }
18 assert res.status() == .ok
19 assert res.body != ''
20 assert res.body.int() > 1566403696
21 println('Current time is: ${res.body.int()}')
22}
23
24fn test_https_get_from_vlang_utc_now() {
25 $if !network ? {
26 return
27 }
28 url := 'https://vlang.io/utc_now'
29 println('Test getting current time from HTTPS ${url} by http.get')
30 res := http.get(url) or { panic(err) }
31 assert res.status() == .ok
32 assert res.body != ''
33 assert res.body.int() > 1566403696
34 println('Current time is: ${res.body.int()}')
35}
36
37fn test_http_public_servers() {
38 $if !network ? {
39 return
40 }
41 urls := [
42 'http://github.com/robots.txt',
43 'http://google.com/robots.txt',
44 // 'http://yahoo.com/robots.txt',
45 ]
46 for url in urls {
47 println('Testing http.get on public HTTP url: ${url} ')
48 res := http.get(url) or { panic(err) }
49 assert res.status() == .ok
50 assert res.body != ''
51 }
52}
53
54fn test_https_public_servers() {
55 $if !network ? {
56 return
57 }
58 urls := [
59 'https://github.com/robots.txt',
60 'https://google.com/robots.txt',
61 // 'https://yahoo.com/robots.txt',
62 ]
63 for url in urls {
64 println('Testing http.get on public HTTPS url: ${url} ')
65 res := http.get(url) or { panic(err) }
66 assert res.status() == .ok
67 assert res.body != ''
68 }
69}
70
71fn test_relative_redirects() {
72 $if !network ? {
73 return
74 }
75 res := http.get('https://httpbin.org/relative-redirect/3?abc=xyz') or { panic(err) }
76 assert res.status() == .ok
77 assert res.body != ''
78 assert res.body.contains('"abc": "xyz"')
79}
80
81fn test_default_user_agent() {
82 $if !network ? {
83 return
84 }
85 res := http.get('https://httpbin.org/user-agent') or { panic(err) }
86 assert res.status() == .ok
87 assert res.body != ''
88 assert res.body.contains('"user-agent": "v.http"')
89}
90
91fn test_custom_user_agent() {
92 $if !network ? {
93 return
94 }
95 ua := 'V http test for UA'
96 mut req := http.new_request(.get, 'https://httpbin.org/user-agent', '')
97 req.user_agent = ua
98 res := req.do() or { panic(err) }
99 assert res.status() == .ok
100 assert res.body != ''
101 assert res.body.contains('"user-agent": "${ua}"')
102}
103