|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { after, before, beforeEach, describe, it } from 'mocha'; |
| 3 | +import fs from 'fs'; |
| 4 | +import os from 'os'; |
| 5 | +import path from 'path'; |
| 6 | + |
| 7 | +import { |
| 8 | + HEADERS_RESOURCE_CACHE_KEY, |
| 9 | + RESOURCE_CACHE_KEY, |
| 10 | + SETTINGS_KEY, |
| 11 | +} from '@/constants'; |
| 12 | + |
| 13 | +let $; |
| 14 | +let openApi; |
| 15 | +let download; |
| 16 | +let resourceCache; |
| 17 | +let headersResourceCache; |
| 18 | +let originalRead; |
| 19 | +let originalWrite; |
| 20 | +let originalInfo; |
| 21 | +let originalError; |
| 22 | +let originalHTTP; |
| 23 | +let originalENV; |
| 24 | +let state; |
| 25 | +let tempDir; |
| 26 | +let previousDataBasePath; |
| 27 | +let capturedUrls; |
| 28 | +let errorLogs; |
| 29 | + |
| 30 | +describe('download github proxy regex', function () { |
| 31 | + before(function () { |
| 32 | + previousDataBasePath = process.env.SUB_STORE_DATA_BASE_PATH; |
| 33 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sub-store-download-')); |
| 34 | + process.env.SUB_STORE_DATA_BASE_PATH = tempDir; |
| 35 | + |
| 36 | + ({ default: $ } = require('@/core/app')); |
| 37 | + openApi = require('@/vendor/open-api'); |
| 38 | + ({ default: resourceCache } = require('@/utils/resource-cache')); |
| 39 | + ({ default: headersResourceCache } = require( |
| 40 | + '@/utils/headers-resource-cache' |
| 41 | + )); |
| 42 | + ({ default: download } = require('@/utils/download')); |
| 43 | + |
| 44 | + originalRead = $.read.bind($); |
| 45 | + originalWrite = $.write.bind($); |
| 46 | + originalInfo = $.info.bind($); |
| 47 | + originalError = $.error.bind($); |
| 48 | + originalHTTP = openApi.HTTP; |
| 49 | + originalENV = openApi.ENV; |
| 50 | + }); |
| 51 | + |
| 52 | + after(function () { |
| 53 | + if ($) { |
| 54 | + $.read = originalRead; |
| 55 | + $.write = originalWrite; |
| 56 | + $.info = originalInfo; |
| 57 | + $.error = originalError; |
| 58 | + } |
| 59 | + |
| 60 | + if (openApi) { |
| 61 | + openApi.HTTP = originalHTTP; |
| 62 | + openApi.ENV = originalENV; |
| 63 | + } |
| 64 | + |
| 65 | + if (previousDataBasePath == null) { |
| 66 | + delete process.env.SUB_STORE_DATA_BASE_PATH; |
| 67 | + } else { |
| 68 | + process.env.SUB_STORE_DATA_BASE_PATH = previousDataBasePath; |
| 69 | + } |
| 70 | + |
| 71 | + if (tempDir) { |
| 72 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + beforeEach(function () { |
| 77 | + capturedUrls = []; |
| 78 | + errorLogs = []; |
| 79 | + state = { |
| 80 | + [SETTINGS_KEY]: { |
| 81 | + githubProxy: 'https://ghproxy.test', |
| 82 | + githubProxyRegex: 'raw\\.githubusercontent\\.com', |
| 83 | + }, |
| 84 | + [RESOURCE_CACHE_KEY]: '{}', |
| 85 | + [HEADERS_RESOURCE_CACHE_KEY]: '{}', |
| 86 | + }; |
| 87 | + |
| 88 | + $.read = (key) => state[key]; |
| 89 | + $.write = (data, key) => { |
| 90 | + state[key] = data; |
| 91 | + return true; |
| 92 | + }; |
| 93 | + $.info = () => {}; |
| 94 | + $.error = (message) => { |
| 95 | + errorLogs.push(message); |
| 96 | + }; |
| 97 | + |
| 98 | + openApi.ENV = () => ({ |
| 99 | + isNode: true, |
| 100 | + isStash: false, |
| 101 | + isLoon: false, |
| 102 | + isShadowRocket: false, |
| 103 | + isQX: false, |
| 104 | + isSurge: false, |
| 105 | + isGUIforCores: false, |
| 106 | + isEgern: false, |
| 107 | + isLanceX: false, |
| 108 | + }); |
| 109 | + openApi.HTTP = () => ({ |
| 110 | + get: async ({ url }) => { |
| 111 | + capturedUrls.push(url); |
| 112 | + return { |
| 113 | + body: 'test-body', |
| 114 | + headers: {}, |
| 115 | + statusCode: 200, |
| 116 | + }; |
| 117 | + }, |
| 118 | + }); |
| 119 | + |
| 120 | + resourceCache.revokeAll(); |
| 121 | + headersResourceCache.revokeAll(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('prefixes matching download urls with the github proxy', async function () { |
| 125 | + await download( |
| 126 | + 'https://raw.githubusercontent.com/sub-store-org/Sub-Store/master/README.md', |
| 127 | + ); |
| 128 | + |
| 129 | + expect(capturedUrls).to.deep.equal([ |
| 130 | + 'https://ghproxy.test/https://raw.githubusercontent.com/sub-store-org/Sub-Store/master/README.md', |
| 131 | + ]); |
| 132 | + }); |
| 133 | + |
| 134 | + it('keeps download urls unchanged when the regex does not match', async function () { |
| 135 | + await download('https://example.com/archive.txt'); |
| 136 | + |
| 137 | + expect(capturedUrls).to.deep.equal([ |
| 138 | + 'https://example.com/archive.txt', |
| 139 | + ]); |
| 140 | + }); |
| 141 | + |
| 142 | + it('matches regex patterns case-insensitively by default', async function () { |
| 143 | + state[SETTINGS_KEY].githubProxyRegex = '^https://RAW\\.GITHUBUSERCONTENT\\.COM'; |
| 144 | + |
| 145 | + await download( |
| 146 | + 'https://raw.githubusercontent.com/sub-store-org/Sub-Store/master/README.md', |
| 147 | + ); |
| 148 | + |
| 149 | + expect(capturedUrls).to.deep.equal([ |
| 150 | + 'https://ghproxy.test/https://raw.githubusercontent.com/sub-store-org/Sub-Store/master/README.md', |
| 151 | + ]); |
| 152 | + }); |
| 153 | + |
| 154 | + it('skips proxy prefixing when the regex is invalid', async function () { |
| 155 | + state[SETTINGS_KEY].githubProxyRegex = '['; |
| 156 | + |
| 157 | + await download( |
| 158 | + 'https://raw.githubusercontent.com/sub-store-org/Sub-Store/master/README.md', |
| 159 | + ); |
| 160 | + |
| 161 | + expect(capturedUrls).to.deep.equal([ |
| 162 | + 'https://raw.githubusercontent.com/sub-store-org/Sub-Store/master/README.md', |
| 163 | + ]); |
| 164 | + expect(errorLogs).to.have.length(1); |
| 165 | + expect(errorLogs[0]).to.contain('GitHub 加速代理匹配正则无效'); |
| 166 | + }); |
| 167 | +}); |
0 commit comments