|
| 1 | +import veb |
| 2 | +import net.http |
| 3 | +import time |
| 4 | +import os |
| 5 | + |
| 6 | +const base_port = 13013 |
| 7 | +const exit_after = time.second * 10 |
| 8 | +const allowed_origin = 'https://vlang.io' |
| 9 | + |
| 10 | +fn get_port_and_url(test_number int) (int, string) { |
| 11 | + p := base_port + test_number |
| 12 | + return p, 'http://localhost:${p}' |
| 13 | +} |
| 14 | + |
| 15 | +pub struct Context { |
| 16 | + veb.Context |
| 17 | +} |
| 18 | + |
| 19 | +pub struct App { |
| 20 | + veb.Middleware[Context] |
| 21 | +mut: |
| 22 | + started chan bool |
| 23 | +} |
| 24 | + |
| 25 | +pub fn (mut app App) before_accept_loop() { |
| 26 | + app.started <- true |
| 27 | +} |
| 28 | + |
| 29 | +pub fn (app &App) index(mut ctx Context) veb.Result { |
| 30 | + return ctx.text('index') |
| 31 | +} |
| 32 | + |
| 33 | +fn setup(port int, o veb.CorsOptions) ! { |
| 34 | + os.chdir(os.dir(@FILE))! |
| 35 | + go fn () { |
| 36 | + time.sleep(exit_after) |
| 37 | + assert false, 'timeout reached!' |
| 38 | + exit(1) |
| 39 | + }() |
| 40 | + |
| 41 | + mut app := &App{} |
| 42 | + app.use(veb.cors[Context](o)) |
| 43 | + |
| 44 | + go veb.run_at[App, Context](mut app, port: port, timeout_in_seconds: 2) |
| 45 | + // app startup time |
| 46 | + _ := <-app.started |
| 47 | +} |
| 48 | + |
| 49 | +fn test_no_user_provided_allowed_headers() { |
| 50 | + port, localserver := get_port_and_url(1) |
| 51 | + setup(port, veb.CorsOptions{ |
| 52 | + origins: [allowed_origin] |
| 53 | + })! |
| 54 | + |
| 55 | + x := http.fetch(http.FetchConfig{ |
| 56 | + url: localserver |
| 57 | + method: http.Method.options |
| 58 | + header: http.new_header_from_map({ |
| 59 | + http.CommonHeader.origin: allowed_origin |
| 60 | + }) |
| 61 | + })! |
| 62 | + |
| 63 | + assert x.status() == http.Status.ok |
| 64 | + if header := x.header.get(.access_control_allow_headers) { |
| 65 | + assert false, 'Header should not be set' |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +fn test_user_provided_allowed_header() { |
| 70 | + port, localserver := get_port_and_url(2) |
| 71 | + setup(port, veb.CorsOptions{ |
| 72 | + origins: [allowed_origin] |
| 73 | + allowed_headers: ['content-type'] |
| 74 | + })! |
| 75 | + |
| 76 | + x := http.fetch(http.FetchConfig{ |
| 77 | + url: localserver |
| 78 | + method: http.Method.options |
| 79 | + header: http.new_header_from_map({ |
| 80 | + http.CommonHeader.origin: allowed_origin |
| 81 | + }) |
| 82 | + })! |
| 83 | + |
| 84 | + assert x.status() == http.Status.ok |
| 85 | + if header := x.header.get(.access_control_allow_headers) { |
| 86 | + assert header == 'content-type' |
| 87 | + } else { |
| 88 | + assert false, 'Header not set' |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +fn test_user_provided_allowed_header_wildcard() { |
| 93 | + port, localserver := get_port_and_url(3) |
| 94 | + setup(port, veb.CorsOptions{ |
| 95 | + origins: [allowed_origin] |
| 96 | + allowed_headers: ['*'] |
| 97 | + })! |
| 98 | + |
| 99 | + x := http.fetch(http.FetchConfig{ |
| 100 | + url: localserver |
| 101 | + method: http.Method.options |
| 102 | + header: http.new_header_from_map({ |
| 103 | + http.CommonHeader.origin: allowed_origin |
| 104 | + }) |
| 105 | + })! |
| 106 | + |
| 107 | + assert x.status() == http.Status.ok |
| 108 | + if header := x.header.get(.access_control_allow_headers) { |
| 109 | + assert header == '*' |
| 110 | + } else { |
| 111 | + assert false, 'Header not set' |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +fn test_request_has_access_control_request_headers() { |
| 116 | + port, localserver := get_port_and_url(4) |
| 117 | + setup(port, veb.CorsOptions{ |
| 118 | + origins: [allowed_origin] |
| 119 | + })! |
| 120 | + |
| 121 | + x := http.fetch(http.FetchConfig{ |
| 122 | + url: localserver |
| 123 | + method: http.Method.options |
| 124 | + header: http.new_header_from_map({ |
| 125 | + http.CommonHeader.origin: allowed_origin |
| 126 | + http.CommonHeader.access_control_request_headers: 'any-value' |
| 127 | + }) |
| 128 | + })! |
| 129 | + |
| 130 | + assert x.status() == http.Status.ok |
| 131 | + if header := x.header.get(http.CommonHeader.access_control_allow_headers) { |
| 132 | + assert header == veb.cors_safelisted_response_headers |
| 133 | + } else { |
| 134 | + assert false, 'Header not set' |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +fn test_allow_credentials_non_preflight() { |
| 139 | + port, localserver := get_port_and_url(5) |
| 140 | + setup(port, veb.CorsOptions{ |
| 141 | + origins: [allowed_origin] |
| 142 | + allowed_methods: [http.Method.get] |
| 143 | + allow_credentials: true |
| 144 | + })! |
| 145 | + |
| 146 | + x := http.fetch(http.FetchConfig{ |
| 147 | + url: localserver |
| 148 | + header: http.new_header_from_map({ |
| 149 | + http.CommonHeader.origin: allowed_origin |
| 150 | + }) |
| 151 | + })! |
| 152 | + |
| 153 | + assert x.status() == http.Status.ok |
| 154 | + if header := x.header.get(http.CommonHeader.access_control_allow_credentials) { |
| 155 | + assert header == 'true' |
| 156 | + } else { |
| 157 | + assert false, 'Header not set' |
| 158 | + } |
| 159 | +} |
0 commit comments