1 // Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
2 package com.intellij.ide.plugins
4 import com.intellij.ide.plugins.auth.PluginRepositoryAuthProvider
5 import com.intellij.ide.plugins.auth.PluginRepositoryAuthService
6 import com.intellij.testFramework.ApplicationRule
7 import com.intellij.testFramework.DisposableRule
8 import com.intellij.testFramework.ExtensionTestUtil
9 import org.junit.ClassRule
12 import kotlin.test.assertEquals
14 class PluginRepositoryAuthServiceTest {
19 val appRule = ApplicationRule()
24 val disposableRule = DisposableRule()
26 private val headers = mapOf("Header" to "DATA")
27 private val fooDomain = "foo.bar"
28 private val fooUrl = "https://foo.bar/foo?a=b&c=d"
29 private val barUrl = "https://bar.baz/baz/zaz"
31 private val unescapedUrl = "https://buildserver.labs.intellij.net/guestAuth/repository/download/Documentation_Stardust/lastSuccessful/updatePlugins.xml?branch=master-internal &build=IU-221.3427.103"
33 private inner class FakeUrlMatchingContributor(private val myDomain: String,
34 private val contributorHeaders: Map<String, String> = headers): PluginRepositoryAuthProvider {
35 override fun canHandle(url: String): Boolean = (url.contains(myDomain))
36 override fun getAuthHeaders(url: String?): Map<String, String> = contributorHeaders
41 setupContributors(FakeUrlMatchingContributor(fooDomain))
42 assertEquals(headers, PluginRepositoryAuthService().getAllCustomHeaders(fooUrl))
46 fun `no headers are returned for an unmatched URL`() {
47 val authService = PluginRepositoryAuthService()
48 setupContributors(FakeUrlMatchingContributor(fooDomain))
49 assertEquals(headers, authService.getAllCustomHeaders(fooUrl))
50 assertEquals(emptyMap(), authService.getAllCustomHeaders(barUrl))
54 fun `only the first contributor is used in case of multiple matching`() {
55 val authService = PluginRepositoryAuthService()
56 setupContributors(FakeUrlMatchingContributor(fooDomain), FakeUrlMatchingContributor(fooDomain, mapOf("foo" to "bar")))
57 assertEquals(headers, authService.getAllCustomHeaders(fooUrl))
61 fun `malformed URL doesn't crash getAllCustomHeaders`() {
62 val authService = PluginRepositoryAuthService()
63 authService.getAllCustomHeaders(unescapedUrl)
67 fun `getAllCustomHeaders doesn't crash when no contributors match`() {
68 val authService = PluginRepositoryAuthService()
69 setupContributors(FakeUrlMatchingContributor(fooDomain))
70 authService.getAllCustomHeaders(barUrl)
71 authService.getAllCustomHeaders(fooUrl)
72 authService.getAllCustomHeaders(barUrl)
73 assertEquals(headers, authService.getAllCustomHeaders(fooUrl))
76 private fun setupContributors(vararg contributor: PluginRepositoryAuthProvider) {
77 ExtensionTestUtil.maskExtensions(PluginRepositoryAuthProvider.EP_NAME, contributor.asList(), disposableRule.disposable)