<commit_before>// Copyright (c) 2013, <NAME>
// For more information, see the LICENSE file

// high level tests.

package spdy_test

import (
	"bytes"
	spdyf "code.google.com/p/go.net/spdy"
	"crypto/tls"
	"fmt"
	"github.com/DanielMorsing/spdy"
	"net"
	"net/http"
	"testing"
	"time"
)

func init() {
	var response = `
	<html>
	<head></head>
	<body>
	<form action="hej" method="post">
	First name: <input type="text" name="firstname"><br>
	Last name: <input type="text" name="lastname">

	<input type="submit" value="Submit"/>
	</form>
	</body>
	</html>
	`
	ds := http.DefaultServeMux
	ds.HandleFunc("/", func(rw http.ResponseWriter, rq *http.Request) {
		rw.WriteHeader(200)
		<-corkch
		rw.Write([]byte(response))
	})
	ds.HandleFunc("/hej", func(rw http.ResponseWriter, rq *http.Request) {
		rq.ParseForm()
		rw.WriteHeader(200)
		fmt.Fprintf(rw, "your name is %s %s\n", rq.Form["firstname"][0], rq.Form["lastname"][0])
	})
	srv := &spdy.Server{
		Addr:    "localhost:4444",
		Handler: ds,
	}
	go srv.ListenAndServe()
	// a complete hack, but it's needed so that the connection isn't refused.
	time.Sleep(200 * time.Millisecond)
}

var corkch = func() chan struct{} {
	c := make(chan struct{})
	close(c)
	return c
}()

func cork() {
	corkch = make(chan struct{})
}

func uncork() {
	corkch <- struct{}{}
	close(corkch)
}

var clientConfig = tls.Config{
	InsecureSkipVerify: true,
	NextProtos:         []string{"spdy/3"},
}

func newClientSession() (*spdyf.Framer, net.Conn) {
	n, err := net.Dial("tcp", "localhost:4444")
	if err != nil {
		panic(err)
	}
	framer, err := spdyf.NewFramer(n, n)
	if err != nil {
		panic(err)
	}
	return framer, n
}

func goldenHeader() http.Header {
	m := make(http.Header)
	m[":host"] = []string{"localhost:4444"}
	m[":path"] = []string{"/"}
	m[":scheme"] = []string{"https"}
	m[":version"] = []string{"HTTP/1.1"}
	m["User-Agent"] = []string{"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"}
	m["Accept"] = []string{"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}
	m[":method"] = []string{"GET"}
	m["Accept-Encoding"] = []string{"gzip,deflate,sdch"}
	m["Accept-Language"] = []string{"en-US,en;q=0.8,da;q=0.6"}
	return m
}

func makeRequest() *spdyf.SynStreamFrame {
	return &spdyf.SynStreamFrame{
		spdyf.ControlFrameHeader{
			Flags: spdyf.ControlFlagFin,
		},
		1,
		0,
		0,
		0,
		goldenHeader(),
	}
}

func goldenSettings() *spdyf.SettingsFrame {
	return &spdyf.SettingsFrame{
		FlagIdValues: []spdyf.SettingsFlagIdValue{
			spdyf.SettingsFlagIdValue{0, 4, 1000},
			spdyf.SettingsFlagIdValue{0, 7, 10485760},
		},
	}
}

func frameRead(t *testing.T, f *spdyf.Framer) spdyf.Frame {
	frame, err := f.ReadFrame()
	if err != nil {
		t.Fatal(err)
	}
	return frame
}

func frameWrite(t *testing.T, f *spdyf.Framer, frame spdyf.Frame) {
	err := f.WriteFrame(frame)
	if err != nil {
		t.Fatal(err)
	}
}

func TestPing(t *testing.T) {
	f, n := newClientSession()
	defer n.Close()

	p := spdyf.PingFrame{Id: 1}
	frameWrite(t, f, &p)

	retf := frameRead(t, f)
	if retp, ok := retf.(*spdyf.PingFrame); ok {
		if retp.Id != 1 {
			t.Error("ping id returned is not the one sent")
		}
	} else {
		t.Error("Received non ping frame")
	}
}

func TestSettings(t *testing.T) {
	f, n := newClientSession()
	defer n.Close()<commit_msg>Ignore window updates in test.<commit_after>// Copyright (c) 2013, <NAME>
// For more information, see the LICENSE file

// high level tests.

package spdy_test

import (
	"bytes"
	spdyf "code.google.com/p/go.net/spdy"
	"crypto/tls"
	"fmt"
	"github.com/DanielMorsing/spdy"
	"net"
	"net/http"
	"testing"
	"time"
)

func init() {
	var response = `
	<html>
	<head></head>
	<body>
	<form action="hej" method="post">
	First name: <input type="text" name="firstname"><br>
	Last name: <input type="text" name="lastname">

	<input type="submit" value="Submit"/>
	</form>
	</body>
	</html>
	`
	ds := http.DefaultServeMux
	ds.HandleFunc("/", func(rw http.ResponseWriter, rq *http.Request) {
		rw.WriteHeader(200)
		<-corkch
		rw.Write([]byte(response))
	})
	ds.HandleFunc("/hej", func(rw http.ResponseWriter, rq *http.Request) {
		rq.ParseForm()
		rw.WriteHeader(200)
		fmt.Fprintf(rw, "your name is %s %s\n", rq.Form["firstname"][0], rq.Form["lastname"][0])
	})
	srv := &spdy.Server{
		Addr:    "localhost:4444",
		Handler: ds,
	}
	go srv.ListenAndServe()
	// a complete hack, but it's needed so that the connection isn't refused.
	time.Sleep(200 * time.Millisecond)
}

var corkch = func() chan struct{} {
	c := make(chan struct{})
	close(c)
	return c
}()

func cork() {
	corkch = make(chan struct{})
}

func uncork() {
	corkch <- struct{}{}
	close(corkch)
}

var clientConfig = tls.Config{
	InsecureSkipVerify: true,
	NextProtos:         []string{"spdy/3"},
}

func newClientSession() (*spdyf.Framer, net.Conn) {
	n, err := net.Dial("tcp", "localhost:4444")
	if err != nil {
		panic(err)
	}
	framer, err := spdyf.NewFramer(n, n)
	if err != nil {
		panic(err)
	}
	return framer, n
}

func goldenHeader() http.Header {
	m := make(http.Header)
	m[":host"] = []string{"localhost:4444"}
	m[":path"] = []string{"/"}
	m[":scheme"] = []string{"https"}
	m[":version"] = []string{"HTTP/1.1"}
	m["User-Agent"] = []string{"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"}
	m["Accept"] = []string{"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}
	m[":method"] = []string{"GET"}
	m["Accept-Encoding"] = []string{"gzip,deflate,sdch"}
	m["Accept-Language"] = []string{"en-US,en;q=0.8,da;q=0.6"}
	return m
}

func makeRequest() *spdyf.SynStreamFrame {
	return &spdyf.SynStreamFrame{
		spdyf.ControlFrameHeader{
			Flags: spdyf.ControlFlagFin,
		},
		1,
		0,
		0,
		0,
		goldenHeader(),
	}
}

func goldenSettings() *spdyf.SettingsFrame {
	return &spdyf.SettingsFrame{
		FlagIdValues: []spdyf.SettingsFlagIdValue{
			spdyf.SettingsFlagIdValue{0, 4, 1000},
			spdyf.SettingsFlagIdValue{0, 7, 10485760},
		},
	}
}

func frameRead(t *testing.T, f *spdyf.Framer) spdyf.Frame {
	for {
		frame, err := f.ReadFrame()
		if err != nil {
			t.Fatal(err)
		}
		if _, ok := frame.(*spdyf.WindowUpdateFrame); !ok {
			return frame
		}
	}

}

func frameWrite(t *testing.T, f *spdyf.Framer, frame spdyf.Frame) {
	err := f.WriteFrame(frame)
	if err != nil {
		t.Fatal(err)
	}
}

func TestPing(t *testing.T) {
	f, n := newClientSession()
	defer n.Close()

	p := spdyf.PingFrame{Id: 1}
	frameWrite(t, f, &p)

	retf := frameRead(t, f)
	if retp, ok := retf.(*spdyf.PingFrame); ok {
		if retp.Id != 1 {
			t.Error("ping id returned is not the one sent")
		}
	} else {
		t.Error("Received non ping frame")
	}
}

func TestSettings(t *testing.T) {
	f, n := newClientSession()
	defer n.Close()
{'fraction_non_alphanumeric': 0.192376149510531, 'fraction_numerical': 0.02580836547018689, 'mean_word_length': 4.054722638680659, 'pattern_counts': {'":': 10, '<': 33, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '4842772', 'n_tokens_mistral': 2990, 'n_tokens_neox': 2795, 'n_words': 580}