<commit_before>package spawn

import (
	"bufio"
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"sort"
	"sync"
	"testing"
	"time"
)

type testAnswer struct {
	Node   string `json:"node"`
	Method string `json:"method"`
	Path   string `json:"path"`
	Data   []byte `json:"data"`
}

type testContent struct {
	Method    string `json:"method"`
	Iteration int    `json:"iteration"`
}

type testUpdatesStats struct {
	mutex    sync.RWMutex
	sent     int
	received map[string]int
}

var stats testUpdatesStats

type testConfig struct {
	Host string `json:"host"`
	Port int    `json:"port"`

	API struct {
		Host string `json:"host"`
		Port int    `json:"port"`
	} `json:"api"`

	Nodes []Node `json:"nodes"`

	FailedNodeID string `json:"failedNodeID"`
}

// LoadConfigFile - loads congig file into config record
func (config *testConfig) loadTestConfig(path string) error {
	_, err := os.Stat(path)
	if err != nil {
		return err
	}
	file, err := os.Open(path)
	if err != nil {
		return err
	}
	defer file.Close()
	if err := json.NewDecoder(bufio.NewReader(file)).Decode(&config); err != nil {
		return err
	}

	return nil
}

func TestServer(t *testing.T) {

	stats.received = make(map[string]int)

	// create new server
	server, err := NewServer("Test-server")
	test(t, err == nil, "Expected create a new server, got", err)
	server.responseTimeout = 1

	// loads test config
	config := &testConfig{}
	err = config.loadTestConfig("fixtures/config.json")
	test(t, err == nil, "Expected loading of the test hosts, got", err)

	// run the server in testing mode
	appHost := fmt.Sprintf("%s:%d", config.Host, config.Port)
	apiHost := fmt.Sprintf("%s:%d", config.API.Host, config.API.Port)
	status, err := server.Run(
		appHost, apiHost, nil, config.Nodes,
		true, true, HealthCheck{Seconds: 1},
	)
	test(t, err == nil, "Expected run the server, got", status, err)<commit_msg>fixed initializing of auth service<commit_after>package spawn

import (
	"bufio"
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"sort"
	"sync"
	"testing"
	"time"

	"github.com/openprovider/spawn/auth"
)

type testAnswer struct {
	Node   string `json:"node"`
	Method string `json:"method"`
	Path   string `json:"path"`
	Data   []byte `json:"data"`
}

type testContent struct {
	Method    string `json:"method"`
	Iteration int    `json:"iteration"`
}

type testUpdatesStats struct {
	mutex    sync.RWMutex
	sent     int
	received map[string]int
}

var stats testUpdatesStats

type testConfig struct {
	Host string `json:"host"`
	Port int    `json:"port"`

	API struct {
		Host string `json:"host"`
		Port int    `json:"port"`
	} `json:"api"`

	Nodes []Node `json:"nodes"`

	AuthEngine auth.AuthConfig `json:"auth"`

	FailedNodeID string `json:"failedNodeID"`
}

// LoadConfigFile - loads congig file into config record
func (config *testConfig) loadTestConfig(path string) error {
	_, err := os.Stat(path)
	if err != nil {
		return err
	}
	file, err := os.Open(path)
	if err != nil {
		return err
	}
	defer file.Close()
	if err := json.NewDecoder(bufio.NewReader(file)).Decode(&config); err != nil {
		return err
	}

	return nil
}

func TestServer(t *testing.T) {

	stats.received = make(map[string]int)

	// create new server
	server, err := NewServer("Test-server")
	test(t, err == nil, "Expected create a new server, got", err)
	server.responseTimeout = 1

	// loads test config
	config := &testConfig{}
	err = config.loadTestConfig("fixtures/config.json")
	test(t, err == nil, "Expected loading of the test hosts, got", err)

	// run the server in testing mode
	appHost := fmt.Sprintf("%s:%d", config.Host, config.Port)
	apiHost := fmt.Sprintf("%s:%d", config.API.Host, config.API.Port)

	// Initialize auth service
	authService, err := auth.NewAuth(&config.AuthEngine)
	test(t, err == nil, "Expected new auth service, got", authService, err)
	status, err := server.Run(
		appHost, apiHost, nil, config.Nodes,
		true, true, HealthCheck{Seconds: 1}, authService,
	)
	test(t, err == nil, "Expected run the server, got", status, err)
{'fraction_non_alphanumeric': 0.14746772591857002, 'fraction_numerical': 0.0009930486593843098, 'mean_word_length': 4.061557788944723, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '6667869', 'n_tokens_mistral': 1440, 'n_tokens_neox': 1399, 'n_words': 402}