(url)
| 88 | } |
| 89 | |
| 90 | getUrlForProxy (url) { |
| 91 | if (url === '') { |
| 92 | url = '/'; |
| 93 | } |
| 94 | const proxyBase = `${this.scheme}://${this.server}:${this.port}${this.base}`; |
| 95 | const endpointRe = '(/(session|status))'; |
| 96 | let remainingUrl = ''; |
| 97 | if (/^http/.test(url)) { |
| 98 | const first = (new RegExp(`(https?://.+)${endpointRe}`)).exec(url); |
| 99 | if (!first) { |
| 100 | throw new Error('Got a complete url but could not extract JWP endpoint'); |
| 101 | } |
| 102 | remainingUrl = url.replace(first[1], ''); |
| 103 | } else if ((new RegExp('^/')).test(url)) { |
| 104 | remainingUrl = url; |
| 105 | } else { |
| 106 | throw new Error(`Did not know what to do with url '${url}'`); |
| 107 | } |
| 108 | |
| 109 | const stripPrefixRe = new RegExp('^.*?(/(session|status).*)$'); |
| 110 | if (stripPrefixRe.test(remainingUrl)) { |
| 111 | remainingUrl = stripPrefixRe.exec(remainingUrl)[1]; |
| 112 | } |
| 113 | |
| 114 | if (!(new RegExp(endpointRe)).test(remainingUrl)) { |
| 115 | remainingUrl = `/session/${this.sessionId}${remainingUrl}`; |
| 116 | } |
| 117 | |
| 118 | const requiresSessionId = this.endpointRequiresSessionId(remainingUrl); |
| 119 | |
| 120 | if (requiresSessionId && this.sessionId === null) { |
| 121 | throw new Error('Trying to proxy a session command without session id'); |
| 122 | } |
| 123 | |
| 124 | const sessionBaseRe = new RegExp('^/session/([^/]+)'); |
| 125 | if (sessionBaseRe.test(remainingUrl)) { |
| 126 | // we have something like /session/:id/foobar, so we need to replace |
| 127 | // the session id |
| 128 | const match = sessionBaseRe.exec(remainingUrl); |
| 129 | remainingUrl = remainingUrl.replace(match[1], this.sessionId); |
| 130 | } else if (requiresSessionId) { |
| 131 | throw new Error(`Could not find :session section for url: ${remainingUrl}`); |
| 132 | } |
| 133 | remainingUrl = remainingUrl.replace(/\/$/, ''); // can't have trailing slashes |
| 134 | |
| 135 | return proxyBase + remainingUrl; |
| 136 | } |
| 137 | |
| 138 | async proxy (url, method, body = null) { |
| 139 | method = method.toUpperCase(); |
no test coverage detected