github.com/alejandroesc/spdy@v0.0.0-20200317064415-01a02f0eb389/regression_test.go (about)

     1  // Copyright 2014 Jamie Hall. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package spdy_test
     6  
     7  import (
     8  	"bytes"
     9  	"io"
    10  	"io/ioutil"
    11  	"net/http"
    12  	"testing"
    13  
    14  	"github.com/SlyMarbo/spdy/common"
    15  )
    16  
    17  func issue82handler(n int64) http.HandlerFunc {
    18  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    19  		w.Header().Set("Content-Type", "text/plain")
    20  		w.Write(bytes.Repeat([]byte{'X'}, int(n)))
    21  	})
    22  }
    23  
    24  func TestIssue82(t *testing.T) {
    25  	var max int64 = common.DEFAULT_INITIAL_CLIENT_WINDOW_SIZE
    26  	tests := []struct {
    27  		Name string
    28  		Path string
    29  		Msg  string
    30  		Want int64
    31  	}{
    32  		{"Issue 82", "/1", "Sending less than window size", max - 16},
    33  		{"Issue 82", "/2", "Sending just under window size", max - 15},
    34  		{"Issue 82", "/3", "Sending exactly window size", max},
    35  		{"Issue 82", "/4", "Sending more than window size", max + 16},
    36  	}
    37  
    38  	// start server
    39  	mux := http.NewServeMux()
    40  	for _, test := range tests {
    41  		mux.HandleFunc(test.Path, issue82handler(test.Want))
    42  	}
    43  
    44  	srv := newServer(mux)
    45  	defer srv.Close()
    46  
    47  	client := newClient()
    48  
    49  	for _, test := range tests {
    50  		r, err := client.Get(srv.URL + test.Path)
    51  		if err != nil {
    52  			t.Fatal(err)
    53  		}
    54  
    55  		n, err := io.Copy(ioutil.Discard, r.Body)
    56  		if err != nil {
    57  			t.Fatal(err)
    58  		}
    59  
    60  		if err = r.Body.Close(); err != nil {
    61  			t.Fatal(err)
    62  		}
    63  
    64  		if n != test.Want {
    65  			t.Errorf("%s: %s, got %d, expected %d", test.Name, test.Msg, n, test.Want)
    66  		}
    67  	}
    68  }