// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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.

// +build !integration

package http

import (
	"bytes"
	"context"
	"crypto/tls"
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"strconv"
	"strings"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"

	"github.com/elastic/beats/v7/metricbeat/helper/server"
)

func TestHTTPServers(t *testing.T) {

	var cases = []struct {
		testName, inputMessage, connectionType, connectionMethod, expectedOutput string
		expectedHTTPCode                                                         int
	}{
		{"HTTP GET", `"@timestamp":"2016-05-23T08:05:34.853Z"`, "HTTP", "GET", "HTTP Server accepts data via POST", 200},
		{"HTTPS GET", `"@timestamp":"2016-05-23T08:05:34.853Z"`, "HTTPS", "GET", "HTTPS Server accepts data via POST", 200},
		{"HTTP POST", `"@timestamp":"2016-05-23T08:05:34.853Z"`, "HTTP", "POST", `"@timestamp":"2016-05-23T08:05:34.853Z"`, 202},
		{"HTTPS POST", `"@timestamp":"2016-05-23T08:05:34.853Z"`, "HTTPS", "POST", `"@timestamp":"2016-05-23T08:05:34.853Z"`, 202},
	}

	for _, test := range cases {
		t.Run(test.testName, func(t *testing.T) {
			host := "127.0.0.1"
			port := 40050
			svc, err := getHTTPServer(t, host, port, test.connectionType)
			if err != nil {
				t.Error(err)
				t.FailNow()
			}

			svc.Start()
			defer svc.Stop()
			// make sure server is up before writing data into it.
			err = checkServerReady(host, port)
			if err != nil {
				t.Error(err)
				t.FailNow()
			}
			httpCode, response := writeToServer(t, test.inputMessage, host, port, test.connectionMethod, test.connectionType)

			assert.True(t, httpCode == test.expectedHTTPCode)

			if test.connectionMethod == "POST" {
				msg := <-svc.GetEvents()

				assert.True(t, msg.GetEvent() != nil)
				ok, _ := msg.GetEvent().HasKey("data")
				assert.True(t, ok)
				bytes, _ := msg.GetEvent()["data"].([]byte)
				httpOutput := string(bytes)
				assert.True(t, httpOutput == test.expectedOutput)
			} else {
				assert.True(t, response == test.expectedOutput)
			}

		})
	}
}

func checkServerReady(host string, port int) error {

	const (
		checkServerReadyTimeout = 5 * time.Second
		checkServerReadyTick    = 100 * time.Millisecond
	)
	var conn net.Conn
	var err error

	ctx, cancel := context.WithTimeout(context.TODO(), checkServerReadyTimeout)
	defer cancel()
	ticker := time.NewTicker(checkServerReadyTick)
	defer ticker.Stop()
	for {
		select {
		case <-ticker.C:
			conn, err = net.Dial("tcp", net.JoinHostPort(host, strconv.Itoa(int(port))))
			if conn != nil {
				_ = conn.Close()
				return nil
			}
			if err != nil {
				return err
			}

		case <-ctx.Done():
			return fmt.Errorf("HTTP server at %s:%d never responded: %+v", host, port, err)
		}
	}

}

func getHTTPServer(t *testing.T, host string, port int, connectionType string) (server.Server, error) {
	ctx, cancel := context.WithCancel(context.Background())
	h := &HttpServer{
		done:       make(chan struct{}),
		eventQueue: make(chan server.Event, 1),
		ctx:        ctx,
		stop:       cancel,
	}
	httpServer := &http.Server{
		Addr:    net.JoinHostPort(host, strconv.Itoa(int(port))),
		Handler: http.HandlerFunc(h.handleFunc),
	}
	if connectionType == "HTTPS" {
		cfg := prepareTLSConfig(t, host)
		httpServer.TLSConfig = cfg
	}
	h.server = httpServer
	return h, nil
}

func prepareTLSConfig(t *testing.T, host string) *tls.Config {
	certPem := []byte(`-----BEGIN CERTIFICATE-----
MIIDwTCCAqmgAwIBAgIJAONBEV813hm6MA0GCSqGSIb3DQEBCwUAMHcxCzAJBgNV
BAYTAkJSMQswCQYDVQQIDAJTUDEPMA0GA1UEBwwGRlJBTkNBMRAwDgYDVQQKDAdF
TEFTVElDMQswCQ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YE3J7m2p2giEbLm+9no5CEmE9T74k3m0eLZug
g/F1MA/evhXEYho6f+lS9Q0ZdtyU2EFrdvuLlUw6FJIWnaOLlVR/aC6BvAlxLDRb
RUGqDKDjl1Die0s8F1aDHGvNvGaZRN4Z23BRPBECgYBE8pMGA8yzlSKui/SiE5iW
UOcVJQ15rWPNBs62KZED5VdFr9cF6Q+DOfxe+ZWk+xHEDSdBWTylYPrgxpb05E6h
vDzpHXfW64AO7jl18LYrQSpJLzvCVkUG4LpcZ+GohAXbSlCJXFB3I1kxvTli+5/K
6tApE8vmpgQI/ZX6+Te4tQKBgBcQ3C1H5voaOf0c4czkCR2tIGQkk2eI/2nipp9O
a053G4PySbEYOOXZopG6wCtV6bwOJNP9xaeTH4S1v4rGwOnQIsofR1BEWMXilCXA
2/4fxesxOsaAxXY3Mqnk1NqovpWDdxXOGf3RaaeR81hV8kGndPYeZJbnE8uQoYTI
586xAoGBAI2SR17xbgfiQBZxgGqamslz4NqBkZUBs4DIAGMAXS21rW/2bbbRaSii
mGmkdaXx+l077AuO0peX2uBvJAx6PvAVW0qroeOLcCo6EuUGTNVhBej6L9hMwhIO
r0tZLlMt75zcnJBicMbIrrzIGVYMHjT+m1QTGbrGb/tcEIGtmXwO
-----END RSA PRIVATE KEY-----`)

	cfg := &tls.Config{
		ServerName:         host,
		MinVersion:         tls.VersionTLS12,
		MaxVersion:         tls.VersionTLS12,
		InsecureSkipVerify: true,
	}
	cfg.Certificates = make([]tls.Certificate, 1)
	cert, err := tls.X509KeyPair(certPem, keyPem)
	if err != nil {
		t.Error(err)
	}
	cfg.Certificates = []tls.Certificate{cert}
	return cfg
}

func writeToServer(t *testing.T, message, host string, port int, connectionMethod string, connectionType string) (int, string) {
	url := fmt.Sprintf("%s://%s:%d/", strings.ToLower(connectionType), host, port)
	var str = []byte(message)
	req, err := http.NewRequest(connectionMethod, url, bytes.NewBuffer(str))
	req.Header.Set("Content-Type", "text/plain")
	client := &http.Client{}
	if connectionType == "HTTPS" {
		client.Transport = &http.Transport{
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: true, // test server certificate is not trusted.
			}}
	}
	resp, err := client.Do(req)
	if err != nil {
		t.Error(err)
		t.FailNow()
	}

	defer resp.Body.Close()

	if connectionMethod == "GET" {
		if resp.StatusCode == http.StatusOK {
			bodyBytes, err2 := ioutil.ReadAll(resp.Body)
			if err2 != nil {
				t.Error(err)
				t.FailNow()
			}
			bodyString := string(bodyBytes)
			return resp.StatusCode, bodyString
		}
	}
	return resp.StatusCode, ""
}
