<commit_before>package fasthttp

import (
	"bufio"
	"bytes"
	"crypto/tls"
	"fmt"
	"io"
	"io/ioutil"
	"net"
	"os"
	"strings"
	"sync"
	"testing"
	"time"

	"github.com/valyala/fasthttp/fasthttputil"
)

func TestServerErrSmallBuffer(t *testing.T) {
	s := &Server{
		Handler: func(ctx *RequestCtx) {
			ctx.WriteString("shouldn't be never called")
		},
		ReadBufferSize: 20,
		Logger:         &customLogger{},
	}
	ln := fasthttputil.NewInmemoryListener()

	serverCh := make(chan error, 1)
	go func() {
		err := s.Serve(ln)
		serverCh <- err
	}()

	clientCh := make(chan error, 1)
	go func() {
		c, err := ln.Dial()
		if err != nil {
			clientCh <- fmt.Errorf("unexpected error: %s", err)
			return
		}
		_, err = c.Write([]byte("GET / HTTP/1.1\r\nHost: aabb.com\r\nVERY-long-Header: sdfdfsd dsf dsaf dsf df fsd\r\n\r\n"))
		if err != nil {
			clientCh <- fmt.Errorf("unexpected error when sending request: %s", err)
			return
		}
		br := bufio.NewReader(c)
		var resp Response
		if err = resp.Read(br); err != nil {
			clientCh <- fmt.Errorf("unexpected error: %s", err)
			return
		}
		statusCode := resp.StatusCode()
		if statusCode != StatusRequestHeaderFieldsTooLarge {
			clientCh <- fmt.Errorf("unexpected status code: %d. Expecting %d", statusCode, StatusRequestHeaderFieldsTooLarge)
			return
		}
		clientCh <- nil
	}()

	var err error

	// wait for the client
	select {
	case <-time.After(time.Second):
		t.Fatalf("timeout when waiting for the client")
	case err = <-clientCh:
		if err != nil {
			t.Fatalf("unexpected client error: %s", err)
		}
	}

	// wait for the server
	if err := ln.Close(); err != nil {
		t.Fatalf("unexpected error: %s", err)
	}
	select {
	case <-time.After(time.Second):
		t.Fatalf("timeout when waiting for the server")
	case err = <-serverCh:
		if err != nil {
			t.Fatalf("unexpected server error: %s", err)
		}
	}
}

func TestRequestCtxIsTLS(t *testing.T) {
	var ctx RequestCtx

	// tls.Conn
	ctx.c = &tls.Conn{}
	if !ctx.IsTLS() {
		t.Fatalf("IsTLS must return true")
	}

	// non-tls.Conn
	ctx.c = &readWriter{}
	if ctx.IsTLS() {
		t.Fatalf("IsTLS must return false")
	}

	// overridden tls.Conn
	ctx.c = &struct {
		*tls.Conn
		fooBar bool<commit_msg>an attempt to localize travis failure on TestServerErrSmallBuffer<commit_after>package fasthttp

import (
	"bufio"
	"bytes"
	"crypto/tls"
	"fmt"
	"io"
	"io/ioutil"
	"net"
	"os"
	"strings"
	"sync"
	"testing"
	"time"

	"github.com/valyala/fasthttp/fasthttputil"
)

func TestServerErrSmallBuffer(t *testing.T) {
	logger := &customLogger{}
	s := &Server{
		Handler: func(ctx *RequestCtx) {
			ctx.WriteString("shouldn't be never called")
		},
		ReadBufferSize: 20,
		Logger:         logger,
	}
	ln := fasthttputil.NewInmemoryListener()

	serverCh := make(chan error, 1)
	go func() {
		err := s.Serve(ln)
		serverCh <- err
	}()

	clientCh := make(chan error, 1)
	go func() {
		c, err := ln.Dial()
		if err != nil {
			clientCh <- fmt.Errorf("unexpected error: %s", err)
			return
		}
		_, err = c.Write([]byte("GET / HTTP/1.1\r\nHost: aabb.com\r\nVERY-long-Header: sdfdfsd dsf dsaf dsf df fsd\r\n\r\n"))
		if err != nil {
			clientCh <- fmt.Errorf("unexpected error when sending request: %s", err)
			return
		}
		br := bufio.NewReader(c)
		var resp Response
		if err = resp.Read(br); err != nil {
			clientCh <- fmt.Errorf("unexpected error: %s", err)
			return
		}
		statusCode := resp.StatusCode()
		if statusCode != StatusRequestHeaderFieldsTooLarge {
			clientCh <- fmt.Errorf("unexpected status code: %d. Expecting %d", statusCode, StatusRequestHeaderFieldsTooLarge)
			return
		}
		clientCh <- nil
	}()

	var err error

	// wait for the client
	select {
	case <-time.After(time.Second):
		t.Fatalf("timeout when waiting for the client. Server log: %q", logger.out)
	case err = <-clientCh:
		if err != nil {
			t.Fatalf("unexpected client error: %s. Server log: %q", err, logger.out)
		}
	}

	// wait for the server
	if err := ln.Close(); err != nil {
		t.Fatalf("unexpected error: %s. Server log: %q", err, logger.out)
	}
	select {
	case <-time.After(time.Second):
		t.Fatalf("timeout when waiting for the server. Server log: %q", logger.out)
	case err = <-serverCh:
		if err != nil {
			t.Fatalf("unexpected server error: %s. Server log: %q", err, logger.out)
		}
	}

	expectedErr := errSmallBuffer.Error()
	if !strings.Contains(logger.out, expectedErr) {
		t.Fatalf("unexpected log output: %q. Expecting %q", logger.out, expectedErr)
	}
}

func TestRequestCtxIsTLS(t *testing.T) {
	var ctx RequestCtx

	// tls.Conn
	ctx.c = &tls.Conn{}
	if !ctx.IsTLS() {
		t.Fatalf("IsTLS must return true")
	}

	// non-tls.Conn
	ctx.c = &readWriter{}
	if ctx.IsTLS() {
		t.Fatalf("IsTLS must return false")
	}

	// overridden tls.Conn
	ctx.c = &struct {
		*tls.Conn
		fooBar bool
{'fraction_non_alphanumeric': 0.15646687697160883, 'fraction_numerical': 0.002523659305993691, 'mean_word_length': 3.3079710144927534, 'pattern_counts': {'":': 0, '<': 23, '<?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': '863086', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1812, 'n_words': 457}