<commit_before>/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gradle.api

import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import org.gradle.integtests.fixtures.TestResources
import org.gradle.test.fixtures.keystore.TestKeyStore
import org.gradle.test.fixtures.server.http.HttpServer
import org.gradle.test.matchers.UserAgentMatcher
import org.gradle.util.GradleVersion
import org.gradle.util.ToBeImplemented
import spock.lang.Issue
import spock.lang.Unroll

class HttpScriptPluginIntegrationSpec extends AbstractIntegrationSpec {
    @org.junit.Rule HttpServer server = new HttpServer()
    @org.junit.Rule TestResources resources = new TestResources(temporaryFolder)

    def setup() {
        settingsFile << "rootProject.name = 'project'"
        server.expectUserAgent(UserAgentMatcher.matchesNameAndVersion("Gradle", GradleVersion.current().getVersion()))
        server.start()
    }

    @Unroll
    def "can apply script via #scheme"() {
        when:
        if (useKeystore) {
            def keyStore = TestKeyStore.init(resources.dir)
            keyStore.enableSslWithServerCert(server)
            keyStore.configureServerCert(executer)
        }

        def script = file('external.gradle')
        server.expectGet('/external.gradle', script)

        script << """
            task doStuff
            assert buildscript.sourceFile == null
            assert "${server.uri}/external.gradle" == buildscript.sourceURI as String
"""

        buildFile << """
            apply from: '$server.uri/external.gradle'
            defaultTasks 'doStuff'
"""

        then:
        succeeds()

        where:
        scheme  | useKeystore
        "http"  | false
        "https" | true
    }

    @ToBeImplemented
    @Issue("https://github.com/gradle/gradle/issues/2891")
    def "can apply script with URI containing a query string"() {
        when:
        def queryString = 'p=foo;a=blob_plain;f=bar;hb=foo/bar/foo'
        def script = file('external.gradle')
        server.expectGetWithQueryString('/external.gradle', queryString, script)

        script << """
            task doStuff
            assert buildscript.sourceFile == null
            assert "${server.uri}/external.gradle?$queryString" == buildscript.sourceURI as String
"""

        buildFile << """
            apply from: '$server.uri/external.gradle?$queryString'
            defaultTasks 'doStuff'
"""

        then:
        // It should succeed
        // succeeds()
        fails()
    }

    def "uses encoding specified by http server"() {
        given:
        executer.withDefaultCharacterEncoding("UTF-8")

        and:
        def scriptFile = file("script-encoding.gradle")
        scriptFile.setText("""
task check {
    doLast {
        assert java.nio.charset.Charset.defaultCharset().name() == "UTF-8"
        // embed a euro character in the text - this is encoded differently in ISO-8859-15 and UTF-8
        assert '\u20AC'.charAt(0) == 0x20AC
    }
}<commit_msg>Fix merge from release<commit_after>/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gradle.api

import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import org.gradle.integtests.fixtures.TestResources
import org.gradle.test.fixtures.keystore.TestKeyStore
import org.gradle.test.fixtures.server.http.HttpServer
import org.gradle.test.matchers.UserAgentMatcher
import org.gradle.util.GradleVersion
import spock.lang.Issue
import spock.lang.Unroll

class HttpScriptPluginIntegrationSpec extends AbstractIntegrationSpec {
    @org.junit.Rule HttpServer server = new HttpServer()
    @org.junit.Rule TestResources resources = new TestResources(temporaryFolder)

    def setup() {
        settingsFile << "rootProject.name = 'project'"
        server.expectUserAgent(UserAgentMatcher.matchesNameAndVersion("Gradle", GradleVersion.current().getVersion()))
        server.start()
    }

    @Unroll
    def "can apply script via #scheme"() {
        when:
        if (useKeystore) {
            def keyStore = TestKeyStore.init(resources.dir)
            keyStore.enableSslWithServerCert(server)
            keyStore.configureServerCert(executer)
        }

        def script = file('external.gradle')
        server.expectGet('/external.gradle', script)

        script << """
            task doStuff
            assert buildscript.sourceFile == null
            assert "${server.uri}/external.gradle" == buildscript.sourceURI as String
"""

        buildFile << """
            apply from: '$server.uri/external.gradle'
            defaultTasks 'doStuff'
"""

        then:
        succeeds()

        where:
        scheme  | useKeystore
        "http"  | false
        "https" | true
    }

    @Issue("https://github.com/gradle/gradle/issues/2891")
    def "can apply script with URI containing a query string"() {
        when:
        def queryString = 'p=foo;a=blob_plain;f=bar;hb=foo/bar/foo'
        def script = file('external.gradle')
        server.expectGetWithQueryString('/external.gradle', queryString, script)

        script << """
            task doStuff
            assert buildscript.sourceFile == null
            assert "${server.uri}/external.gradle?$queryString" == buildscript.sourceURI as String
"""

        buildFile << """
            apply from: '$server.uri/external.gradle?$queryString'
            defaultTasks 'doStuff'
"""

        then:
        succeeds()
    }

    def "scripts with same URI path but different query strings are treated as separate things"() {
        when:
        def first = file('first.gradle') << """
            task first
            assert buildscript.sourceFile == null
            assert "${server.uri}/external.gradle?first" == buildscript.sourceURI as String
        """
        def second = file('second.gradle') << """
            task second
            assert buildscript.sourceFile == null
            assert "${server.uri}/external.gradle?second" == buildscript.sourceURI as String
        """
        server.expectGetWithQueryString('/external.gradle', "first", first)
        server.expectGetWithQueryString('/external.gradle', "second", second)

        buildFile << """
            apply from: '$server.uri/external.gradle?first'
            apply from: '$server.uri/external.gradle?second'
            defaultTasks 'first', 'second'
"""

        then:
        succeeds()
    }

    def "does not cache URIs with query parts"() {
        when:
        def queryString = 'p=foo;a=blob_plain;f=bar;hb=foo/bar/foo'
        def script = file('external.gradle')
        server.expectGetWithQueryString('/external.gradle', queryString, script)

        script << """
            task doStuff
            assert buildscript.sourceFile == null
            assert "${server.uri}/external.gradle?$queryString" == buildscript.sourceURI as String
"""

        buildFile << """
            apply from: '$server.uri/external.gradle?$queryString'
            defaultTasks 'doStuff'
"""

        then:
        succeeds()

        when:
        server.expectGetWithQueryString('/external.gradle', queryString, script)
        then:
        succeeds()

        when:
        server.stop()
        then:
        fails()
    }

    def "reasonable error message while --offline when applying a script with a query part"() {
        def url = "$server.uri/external.gradle?query"
        buildFile << """
            apply from: '$url'
            defaultTasks 'doStuff'
        """
        server.stop()
        expect:
        fails("--offline")
        result.error.contains("Could not read script '$url'")
    }

    def "uses encoding specified by http server"() {
        given:
        executer.withDefaultCharacterEncoding("UTF-8")

        and:
        def scriptFile = file("script-encoding.gradle")
        scriptFile.setText("""
task check {
    doLast {
        assert java.nio.charset.Charset.defaultCharset().name() == "UTF-8"
        // embed a euro character in the text - this is encoded differently in ISO-8859-15 and UTF-8
        assert '\u20AC'.charAt(0) == 0x20AC
    }
}
{'fraction_non_alphanumeric': 0.10809047411918225, 'fraction_numerical': 0.0058721183123097, 'mean_word_length': 2.538668718737976, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6004637', 'n_tokens_mistral': 2564, 'n_tokens_neox': 2434, 'n_words': 648}