<commit_before>package cmd

import (
	"net/http"
	"os"
	"testing"

	"github.com/elazarl/goproxy"
	"github.com/tj/assert"
)

type footprints struct {
	http  int
	https int
}

func setupProxyServerForTest(proxyAddr string, fp *footprints) {
	proxy := goproxy.NewProxyHttpServer()
	proxy.Verbose = true
	proxy.Tr = &http.Transport{} // To avoid using this proxy to access this proxy itself

	proxy.OnRequest(goproxy.DstHostIs("example.com")).DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
		fp.http++
		return req, nil
	})

	proxy.OnRequest().HandleConnect(goproxy.FuncHttpsHandler(func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
		fp.https++
		return goproxy.RejectConnect, host
	}))

	go http.ListenAndServe(proxyAddr, proxy)
}

func TestCallAPIWithProxy(t *testing.T) {
	origEnvVars := saveEnvVars([]string{"HTTP_PROXY"})
	defer restoreEnvVars(origEnvVars)

	proxyPort := ":18080"
	proxyAddr := "http://localhost" + proxyPort
	err := os.Setenv("HTTP_PROXY", proxyAddr)
	assert.NoError(t, err)
	err = os.Setenv("HTTPS_PROXY", proxyAddr)
	assert.NoError(t, err)

	fp := &footprints{}
	setupProxyServerForTest(proxyPort, fp)

	// Test HTTP
	ac := newAPIClient(&apiClientOptions{Endpoint: "http://example.com"})
	// Ignoring response and error because example.com always returns 404 Not Found for the path 'v1/subscribers'
	_, _ = ac.callAPI(&apiParams{
		method:         "GET",
		path:           "v1/subscribers",
		noRetryOnError: true,
	})
	//assert.Equal(t, 1, fp.http, "proxy server should be accessed when HTTP_PROXY env var is set")

	// Test HTTPS
	ac = newAPIClient(&apiClientOptions{Endpoint: "https://api.soracom.io"})
	// Ignoring response and error because api.soracom.io always returns 400 Bad request with the error message 'invalid api key'
	_, _ = ac.callAPI(&apiParams{
		method:         "GET",
		path:           "v1/subscribers",
		noRetryOnError: true,
	})
	assert.Equal(t, 1, fp.https, "proxy server should be accessed when HTTP_PROXY env var is set")

}

func saveEnvVars(vars []string) map[string]string {
	result := make(map[string]string)
	for _, v := range vars {
		result[v] = os.Getenv(v)
	}
	return result
}

func restoreEnvVars(origVars map[string]string) {
	for k, v := range origVars {
		os.Setenv(k, v)
	}
}

func TestConcatJSONArray(t *testing.T) {
	var testData = []struct {
		Name     string
		Arr1     string<commit_msg>[modify] revert temporaly comment out in test<commit_after>package cmd

import (
	"net/http"
	"os"
	"testing"

	"github.com/elazarl/goproxy"
	"github.com/tj/assert"
)

type footprints struct {
	http  int
	https int
}

func setupProxyServerForTest(proxyAddr string, fp *footprints) {
	proxy := goproxy.NewProxyHttpServer()
	proxy.Verbose = true
	proxy.Tr = &http.Transport{} // To avoid using this proxy to access this proxy itself

	proxy.OnRequest(goproxy.DstHostIs("example.com")).DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
		fp.http++
		return req, nil
	})

	proxy.OnRequest().HandleConnect(goproxy.FuncHttpsHandler(func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
		fp.https++
		return goproxy.RejectConnect, host
	}))

	go http.ListenAndServe(proxyAddr, proxy)
}

func TestCallAPIWithProxy(t *testing.T) {
	origEnvVars := saveEnvVars([]string{"HTTP_PROXY"})
	defer restoreEnvVars(origEnvVars)

	proxyPort := ":18080"
	proxyAddr := "http://localhost" + proxyPort
	err := os.Setenv("HTTP_PROXY", proxyAddr)
	assert.NoError(t, err)
	err = os.Setenv("HTTPS_PROXY", proxyAddr)
	assert.NoError(t, err)

	fp := &footprints{}
	setupProxyServerForTest(proxyPort, fp)

	// Test HTTP
	ac := newAPIClient(&apiClientOptions{Endpoint: "http://example.com"})
	// Ignoring response and error because example.com always returns 404 Not Found for the path 'v1/subscribers'
	_, _ = ac.callAPI(&apiParams{
		method:         "GET",
		path:           "v1/subscribers",
		noRetryOnError: true,
	})
	assert.Equal(t, 1, fp.http, "proxy server should be accessed when HTTP_PROXY env var is set")

	// Test HTTPS
	ac = newAPIClient(&apiClientOptions{Endpoint: "https://api.soracom.io"})
	// Ignoring response and error because api.soracom.io always returns 400 Bad request with the error message 'invalid api key'
	_, _ = ac.callAPI(&apiParams{
		method:         "GET",
		path:           "v1/subscribers",
		noRetryOnError: true,
	})
	assert.Equal(t, 1, fp.https, "proxy server should be accessed when HTTP_PROXY env var is set")

}

func saveEnvVars(vars []string) map[string]string {
	result := make(map[string]string)
	for _, v := range vars {
		result[v] = os.Getenv(v)
	}
	return result
}

func restoreEnvVars(origVars map[string]string) {
	for k, v := range origVars {
		os.Setenv(k, v)
	}
}

func TestConcatJSONArray(t *testing.T) {
	var testData = []struct {
		Name     string
		Arr1     string
{'fraction_non_alphanumeric': 0.13130898645876077, 'fraction_numerical': 0.006975789905621666, 'mean_word_length': 4.293159609120521, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '2258481', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1742, 'n_words': 413}