use of org.zaproxy.zap.extension.script.ScriptsCache.CachedScript in project zaproxy by zaproxy.
the class ScriptsCacheUnitTest method shouldCacheEnabledScripts.
@Test
void shouldCacheEnabledScripts() throws Exception {
// Given
Script script1 = mock(Script.class);
ScriptWrapper scriptWrapper1 = mockScriptWrapper(script1);
Script script2 = mock(Script.class);
ScriptWrapper scriptWrapper2 = mockScriptWrapper(script2);
given(extensionScript.getScripts(scriptType)).willReturn(asList(scriptWrapper1, scriptWrapper2));
// When
scriptsCache.refresh();
// Then
List<CachedScript<Script>> cachedScripts = scriptsCache.getCachedScripts();
assertThat(cachedScripts, hasSize(2));
assertCachedScript(cachedScripts.get(0), scriptWrapper1, script1);
assertCachedScript(cachedScripts.get(1), scriptWrapper2, script2);
verify(extensionScript, times(1)).getScripts(scriptType);
verify(extensionScript, times(1)).getInterface(scriptWrapper1, targetInterface);
verify(extensionScript, times(1)).getInterface(scriptWrapper2, targetInterface);
verifyNoMoreInteractions(extensionScript);
}
use of org.zaproxy.zap.extension.script.ScriptsCache.CachedScript in project zaproxy by zaproxy.
the class ScriptsCacheUnitTest method shouldNotCacheScriptsThatDoNotImplementInterface.
@Test
void shouldNotCacheScriptsThatDoNotImplementInterface() throws Exception {
// Given
Script script1 = mock(Script.class);
ScriptWrapper scriptWrapper1 = mockScriptWrapper(script1);
Script script2 = mock(Script.class);
ScriptWrapper scriptWrapper2 = mockScriptWrapper(script2);
given(extensionScript.getInterface(scriptWrapper2, targetInterface)).willReturn(null);
given(extensionScript.getScripts(scriptType)).willReturn(asList(scriptWrapper1, scriptWrapper2));
// When
scriptsCache.refresh();
// Then
List<CachedScript<Script>> cachedScripts = scriptsCache.getCachedScripts();
assertThat(cachedScripts, hasSize(1));
assertCachedScript(cachedScripts.get(0), scriptWrapper1, script1);
verify(extensionScript, times(1)).getScripts(scriptType);
verify(extensionScript, times(1)).getInterface(scriptWrapper1, targetInterface);
verify(extensionScript, times(1)).getInterface(scriptWrapper2, targetInterface);
verify(extensionScript, times(1)).handleFailedScriptInterface(scriptWrapper2, interfaceErrorMessage);
verifyNoMoreInteractions(extensionScript);
}
use of org.zaproxy.zap.extension.script.ScriptsCache.CachedScript in project zaproxy by zaproxy.
the class ScriptsCacheUnitTest method shouldCacheEnabledScriptsAndIgnoreDisabledScriptsWhenRefreshing.
@Test
void shouldCacheEnabledScriptsAndIgnoreDisabledScriptsWhenRefreshing() throws Exception {
// Given
Script script1 = mock(Script.class);
ScriptWrapper scriptWrapper1 = mockScriptWrapper(script1);
given(scriptWrapper1.isEnabled()).willReturn(false);
Script script2 = mock(Script.class);
ScriptWrapper scriptWrapper2 = mockScriptWrapper(script2);
given(extensionScript.getScripts(scriptType)).willReturn(asList(scriptWrapper1, scriptWrapper2));
// When
scriptsCache.refresh();
// Then
List<CachedScript<Script>> cachedScripts = scriptsCache.getCachedScripts();
assertThat(cachedScripts, hasSize(1));
assertCachedScript(cachedScripts.get(0), scriptWrapper2, script2);
verify(extensionScript, times(1)).getScripts(scriptType);
verify(extensionScript, times(1)).getInterface(scriptWrapper2, targetInterface);
verifyNoMoreInteractions(extensionScript);
}
use of org.zaproxy.zap.extension.script.ScriptsCache.CachedScript in project zaproxy by zaproxy.
the class ScriptsCacheUnitTest method shouldRefreshCachedScriptIfChanged.
@Test
void shouldRefreshCachedScriptIfChanged() throws Exception {
// Given
Script script1 = mock(Script.class);
ScriptWrapper scriptWrapper1 = mockScriptWrapper(script1);
Script script2 = mock(Script.class);
ScriptWrapper scriptWrapper2 = mockScriptWrapper(script2);
given(extensionScript.getScripts(scriptType)).willReturn(asList(scriptWrapper1, scriptWrapper2));
// When
scriptsCache.refresh();
given(scriptWrapper2.getModCount()).willReturn(1);
Script refreshedScript = mock(Script.class);
given(extensionScript.getInterface(scriptWrapper2, targetInterface)).willReturn(refreshedScript);
scriptsCache.refresh();
// Then
List<CachedScript<Script>> cachedScripts = scriptsCache.getCachedScripts();
assertThat(cachedScripts, hasSize(2));
assertCachedScript(cachedScripts.get(0), scriptWrapper1, script1);
assertCachedScript(cachedScripts.get(1), scriptWrapper2, refreshedScript);
verify(extensionScript, times(2)).getScripts(scriptType);
verify(extensionScript, times(1)).getInterface(scriptWrapper1, targetInterface);
verify(extensionScript, times(2)).getInterface(scriptWrapper2, targetInterface);
verifyNoMoreInteractions(extensionScript);
}
use of org.zaproxy.zap.extension.script.ScriptsCache.CachedScript in project zaproxy by zaproxy.
the class ScriptsCacheUnitTest method shouldNotCacheScriptsNoLongerEnabledWhenRefreshing.
@Test
void shouldNotCacheScriptsNoLongerEnabledWhenRefreshing() throws Exception {
// Given
Script script1 = mock(Script.class);
ScriptWrapper scriptWrapper1 = mockScriptWrapper(script1);
Script script2 = mock(Script.class);
ScriptWrapper scriptWrapper2 = mockScriptWrapper(script2);
given(extensionScript.getScripts(scriptType)).willReturn(asList(scriptWrapper1, scriptWrapper2));
// When
scriptsCache.refresh();
given(scriptWrapper1.isEnabled()).willReturn(false);
scriptsCache.refresh();
// Then
List<CachedScript<Script>> cachedScripts = scriptsCache.getCachedScripts();
assertThat(cachedScripts, hasSize(1));
assertCachedScript(cachedScripts.get(0), scriptWrapper2, script2);
verify(extensionScript, times(2)).getScripts(scriptType);
verify(extensionScript, times(1)).getInterface(scriptWrapper1, targetInterface);
verify(extensionScript, times(1)).getInterface(scriptWrapper2, targetInterface);
verifyNoMoreInteractions(extensionScript);
}
Aggregations