transport_test.go 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281
  1. // Copyright 2015 The Go Authors. 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. package http2
  5. import (
  6. "bufio"
  7. "bytes"
  8. "context"
  9. "crypto/tls"
  10. "errors"
  11. "flag"
  12. "fmt"
  13. "io"
  14. "io/ioutil"
  15. "log"
  16. "math/rand"
  17. "net"
  18. "net/http"
  19. "net/http/httptest"
  20. "net/textproto"
  21. "net/url"
  22. "os"
  23. "reflect"
  24. "runtime"
  25. "sort"
  26. "strconv"
  27. "strings"
  28. "sync"
  29. "sync/atomic"
  30. "testing"
  31. "time"
  32. "golang.org/x/net/http2/hpack"
  33. )
  34. var (
  35. extNet = flag.Bool("extnet", false, "do external network tests")
  36. transportHost = flag.String("transporthost", "http2.golang.org", "hostname to use for TestTransport")
  37. insecure = flag.Bool("insecure", false, "insecure TLS dials") // TODO: dead code. remove?
  38. )
  39. var tlsConfigInsecure = &tls.Config{InsecureSkipVerify: true}
  40. var canceledCtx context.Context
  41. func init() {
  42. ctx, cancel := context.WithCancel(context.Background())
  43. cancel()
  44. canceledCtx = ctx
  45. }
  46. func TestTransportExternal(t *testing.T) {
  47. if !*extNet {
  48. t.Skip("skipping external network test")
  49. }
  50. req, _ := http.NewRequest("GET", "https://"+*transportHost+"/", nil)
  51. rt := &Transport{TLSClientConfig: tlsConfigInsecure}
  52. res, err := rt.RoundTrip(req)
  53. if err != nil {
  54. t.Fatalf("%v", err)
  55. }
  56. res.Write(os.Stdout)
  57. }
  58. type fakeTLSConn struct {
  59. net.Conn
  60. }
  61. func (c *fakeTLSConn) ConnectionState() tls.ConnectionState {
  62. return tls.ConnectionState{
  63. Version: tls.VersionTLS12,
  64. CipherSuite: cipher_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  65. }
  66. }
  67. func startH2cServer(t *testing.T) net.Listener {
  68. h2Server := &Server{}
  69. l := newLocalListener(t)
  70. go func() {
  71. conn, err := l.Accept()
  72. if err != nil {
  73. t.Error(err)
  74. return
  75. }
  76. h2Server.ServeConn(&fakeTLSConn{conn}, &ServeConnOpts{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  77. fmt.Fprintf(w, "Hello, %v, http: %v", r.URL.Path, r.TLS == nil)
  78. })})
  79. }()
  80. return l
  81. }
  82. func TestTransportH2c(t *testing.T) {
  83. l := startH2cServer(t)
  84. defer l.Close()
  85. req, err := http.NewRequest("GET", "http://"+l.Addr().String()+"/foobar", nil)
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. tr := &Transport{
  90. AllowHTTP: true,
  91. DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
  92. return net.Dial(network, addr)
  93. },
  94. }
  95. res, err := tr.RoundTrip(req)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. if res.ProtoMajor != 2 {
  100. t.Fatal("proto not h2c")
  101. }
  102. body, err := ioutil.ReadAll(res.Body)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. if got, want := string(body), "Hello, /foobar, http: true"; got != want {
  107. t.Fatalf("response got %v, want %v", got, want)
  108. }
  109. }
  110. func TestTransport(t *testing.T) {
  111. const body = "sup"
  112. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  113. io.WriteString(w, body)
  114. }, optOnlyServer)
  115. defer st.Close()
  116. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  117. defer tr.CloseIdleConnections()
  118. req, err := http.NewRequest("GET", st.ts.URL, nil)
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. res, err := tr.RoundTrip(req)
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. defer res.Body.Close()
  127. t.Logf("Got res: %+v", res)
  128. if g, w := res.StatusCode, 200; g != w {
  129. t.Errorf("StatusCode = %v; want %v", g, w)
  130. }
  131. if g, w := res.Status, "200 OK"; g != w {
  132. t.Errorf("Status = %q; want %q", g, w)
  133. }
  134. wantHeader := http.Header{
  135. "Content-Length": []string{"3"},
  136. "Content-Type": []string{"text/plain; charset=utf-8"},
  137. "Date": []string{"XXX"}, // see cleanDate
  138. }
  139. cleanDate(res)
  140. if !reflect.DeepEqual(res.Header, wantHeader) {
  141. t.Errorf("res Header = %v; want %v", res.Header, wantHeader)
  142. }
  143. if res.Request != req {
  144. t.Errorf("Response.Request = %p; want %p", res.Request, req)
  145. }
  146. if res.TLS == nil {
  147. t.Error("Response.TLS = nil; want non-nil")
  148. }
  149. slurp, err := ioutil.ReadAll(res.Body)
  150. if err != nil {
  151. t.Errorf("Body read: %v", err)
  152. } else if string(slurp) != body {
  153. t.Errorf("Body = %q; want %q", slurp, body)
  154. }
  155. }
  156. func onSameConn(t *testing.T, modReq func(*http.Request)) bool {
  157. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  158. io.WriteString(w, r.RemoteAddr)
  159. }, optOnlyServer, func(c net.Conn, st http.ConnState) {
  160. t.Logf("conn %v is now state %v", c.RemoteAddr(), st)
  161. })
  162. defer st.Close()
  163. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  164. defer tr.CloseIdleConnections()
  165. get := func() string {
  166. req, err := http.NewRequest("GET", st.ts.URL, nil)
  167. if err != nil {
  168. t.Fatal(err)
  169. }
  170. modReq(req)
  171. res, err := tr.RoundTrip(req)
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. defer res.Body.Close()
  176. slurp, err := ioutil.ReadAll(res.Body)
  177. if err != nil {
  178. t.Fatalf("Body read: %v", err)
  179. }
  180. addr := strings.TrimSpace(string(slurp))
  181. if addr == "" {
  182. t.Fatalf("didn't get an addr in response")
  183. }
  184. return addr
  185. }
  186. first := get()
  187. second := get()
  188. return first == second
  189. }
  190. func TestTransportReusesConns(t *testing.T) {
  191. if !onSameConn(t, func(*http.Request) {}) {
  192. t.Errorf("first and second responses were on different connections")
  193. }
  194. }
  195. func TestTransportReusesConn_RequestClose(t *testing.T) {
  196. if onSameConn(t, func(r *http.Request) { r.Close = true }) {
  197. t.Errorf("first and second responses were not on different connections")
  198. }
  199. }
  200. func TestTransportReusesConn_ConnClose(t *testing.T) {
  201. if onSameConn(t, func(r *http.Request) { r.Header.Set("Connection", "close") }) {
  202. t.Errorf("first and second responses were not on different connections")
  203. }
  204. }
  205. // Tests that the Transport only keeps one pending dial open per destination address.
  206. // https://golang.org/issue/13397
  207. func TestTransportGroupsPendingDials(t *testing.T) {
  208. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  209. io.WriteString(w, r.RemoteAddr)
  210. }, optOnlyServer)
  211. defer st.Close()
  212. tr := &Transport{
  213. TLSClientConfig: tlsConfigInsecure,
  214. }
  215. defer tr.CloseIdleConnections()
  216. var (
  217. mu sync.Mutex
  218. dials = map[string]int{}
  219. )
  220. var wg sync.WaitGroup
  221. for i := 0; i < 10; i++ {
  222. wg.Add(1)
  223. go func() {
  224. defer wg.Done()
  225. req, err := http.NewRequest("GET", st.ts.URL, nil)
  226. if err != nil {
  227. t.Error(err)
  228. return
  229. }
  230. res, err := tr.RoundTrip(req)
  231. if err != nil {
  232. t.Error(err)
  233. return
  234. }
  235. defer res.Body.Close()
  236. slurp, err := ioutil.ReadAll(res.Body)
  237. if err != nil {
  238. t.Errorf("Body read: %v", err)
  239. }
  240. addr := strings.TrimSpace(string(slurp))
  241. if addr == "" {
  242. t.Errorf("didn't get an addr in response")
  243. }
  244. mu.Lock()
  245. dials[addr]++
  246. mu.Unlock()
  247. }()
  248. }
  249. wg.Wait()
  250. if len(dials) != 1 {
  251. t.Errorf("saw %d dials; want 1: %v", len(dials), dials)
  252. }
  253. tr.CloseIdleConnections()
  254. if err := retry(50, 10*time.Millisecond, func() error {
  255. cp, ok := tr.connPool().(*clientConnPool)
  256. if !ok {
  257. return fmt.Errorf("Conn pool is %T; want *clientConnPool", tr.connPool())
  258. }
  259. cp.mu.Lock()
  260. defer cp.mu.Unlock()
  261. if len(cp.dialing) != 0 {
  262. return fmt.Errorf("dialing map = %v; want empty", cp.dialing)
  263. }
  264. if len(cp.conns) != 0 {
  265. return fmt.Errorf("conns = %v; want empty", cp.conns)
  266. }
  267. if len(cp.keys) != 0 {
  268. return fmt.Errorf("keys = %v; want empty", cp.keys)
  269. }
  270. return nil
  271. }); err != nil {
  272. t.Errorf("State of pool after CloseIdleConnections: %v", err)
  273. }
  274. }
  275. func retry(tries int, delay time.Duration, fn func() error) error {
  276. var err error
  277. for i := 0; i < tries; i++ {
  278. err = fn()
  279. if err == nil {
  280. return nil
  281. }
  282. time.Sleep(delay)
  283. }
  284. return err
  285. }
  286. func TestTransportAbortClosesPipes(t *testing.T) {
  287. shutdown := make(chan struct{})
  288. st := newServerTester(t,
  289. func(w http.ResponseWriter, r *http.Request) {
  290. w.(http.Flusher).Flush()
  291. <-shutdown
  292. },
  293. optOnlyServer,
  294. )
  295. defer st.Close()
  296. defer close(shutdown) // we must shutdown before st.Close() to avoid hanging
  297. done := make(chan struct{})
  298. requestMade := make(chan struct{})
  299. go func() {
  300. defer close(done)
  301. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  302. req, err := http.NewRequest("GET", st.ts.URL, nil)
  303. if err != nil {
  304. t.Fatal(err)
  305. }
  306. res, err := tr.RoundTrip(req)
  307. if err != nil {
  308. t.Fatal(err)
  309. }
  310. defer res.Body.Close()
  311. close(requestMade)
  312. _, err = ioutil.ReadAll(res.Body)
  313. if err == nil {
  314. t.Error("expected error from res.Body.Read")
  315. }
  316. }()
  317. <-requestMade
  318. // Now force the serve loop to end, via closing the connection.
  319. st.closeConn()
  320. // deadlock? that's a bug.
  321. select {
  322. case <-done:
  323. case <-time.After(3 * time.Second):
  324. t.Fatal("timeout")
  325. }
  326. }
  327. // TODO: merge this with TestTransportBody to make TestTransportRequest? This
  328. // could be a table-driven test with extra goodies.
  329. func TestTransportPath(t *testing.T) {
  330. gotc := make(chan *url.URL, 1)
  331. st := newServerTester(t,
  332. func(w http.ResponseWriter, r *http.Request) {
  333. gotc <- r.URL
  334. },
  335. optOnlyServer,
  336. )
  337. defer st.Close()
  338. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  339. defer tr.CloseIdleConnections()
  340. const (
  341. path = "/testpath"
  342. query = "q=1"
  343. )
  344. surl := st.ts.URL + path + "?" + query
  345. req, err := http.NewRequest("POST", surl, nil)
  346. if err != nil {
  347. t.Fatal(err)
  348. }
  349. c := &http.Client{Transport: tr}
  350. res, err := c.Do(req)
  351. if err != nil {
  352. t.Fatal(err)
  353. }
  354. defer res.Body.Close()
  355. got := <-gotc
  356. if got.Path != path {
  357. t.Errorf("Read Path = %q; want %q", got.Path, path)
  358. }
  359. if got.RawQuery != query {
  360. t.Errorf("Read RawQuery = %q; want %q", got.RawQuery, query)
  361. }
  362. }
  363. func randString(n int) string {
  364. rnd := rand.New(rand.NewSource(int64(n)))
  365. b := make([]byte, n)
  366. for i := range b {
  367. b[i] = byte(rnd.Intn(256))
  368. }
  369. return string(b)
  370. }
  371. type panicReader struct{}
  372. func (panicReader) Read([]byte) (int, error) { panic("unexpected Read") }
  373. func (panicReader) Close() error { panic("unexpected Close") }
  374. func TestActualContentLength(t *testing.T) {
  375. tests := []struct {
  376. req *http.Request
  377. want int64
  378. }{
  379. // Verify we don't read from Body:
  380. 0: {
  381. req: &http.Request{Body: panicReader{}},
  382. want: -1,
  383. },
  384. // nil Body means 0, regardless of ContentLength:
  385. 1: {
  386. req: &http.Request{Body: nil, ContentLength: 5},
  387. want: 0,
  388. },
  389. // ContentLength is used if set.
  390. 2: {
  391. req: &http.Request{Body: panicReader{}, ContentLength: 5},
  392. want: 5,
  393. },
  394. // http.NoBody means 0, not -1.
  395. 3: {
  396. req: &http.Request{Body: http.NoBody},
  397. want: 0,
  398. },
  399. }
  400. for i, tt := range tests {
  401. got := actualContentLength(tt.req)
  402. if got != tt.want {
  403. t.Errorf("test[%d]: got %d; want %d", i, got, tt.want)
  404. }
  405. }
  406. }
  407. func TestTransportBody(t *testing.T) {
  408. bodyTests := []struct {
  409. body string
  410. noContentLen bool
  411. }{
  412. {body: "some message"},
  413. {body: "some message", noContentLen: true},
  414. {body: strings.Repeat("a", 1<<20), noContentLen: true},
  415. {body: strings.Repeat("a", 1<<20)},
  416. {body: randString(16<<10 - 1)},
  417. {body: randString(16 << 10)},
  418. {body: randString(16<<10 + 1)},
  419. {body: randString(512<<10 - 1)},
  420. {body: randString(512 << 10)},
  421. {body: randString(512<<10 + 1)},
  422. {body: randString(1<<20 - 1)},
  423. {body: randString(1 << 20)},
  424. {body: randString(1<<20 + 2)},
  425. }
  426. type reqInfo struct {
  427. req *http.Request
  428. slurp []byte
  429. err error
  430. }
  431. gotc := make(chan reqInfo, 1)
  432. st := newServerTester(t,
  433. func(w http.ResponseWriter, r *http.Request) {
  434. slurp, err := ioutil.ReadAll(r.Body)
  435. if err != nil {
  436. gotc <- reqInfo{err: err}
  437. } else {
  438. gotc <- reqInfo{req: r, slurp: slurp}
  439. }
  440. },
  441. optOnlyServer,
  442. )
  443. defer st.Close()
  444. for i, tt := range bodyTests {
  445. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  446. defer tr.CloseIdleConnections()
  447. var body io.Reader = strings.NewReader(tt.body)
  448. if tt.noContentLen {
  449. body = struct{ io.Reader }{body} // just a Reader, hiding concrete type and other methods
  450. }
  451. req, err := http.NewRequest("POST", st.ts.URL, body)
  452. if err != nil {
  453. t.Fatalf("#%d: %v", i, err)
  454. }
  455. c := &http.Client{Transport: tr}
  456. res, err := c.Do(req)
  457. if err != nil {
  458. t.Fatalf("#%d: %v", i, err)
  459. }
  460. defer res.Body.Close()
  461. ri := <-gotc
  462. if ri.err != nil {
  463. t.Errorf("#%d: read error: %v", i, ri.err)
  464. continue
  465. }
  466. if got := string(ri.slurp); got != tt.body {
  467. t.Errorf("#%d: Read body mismatch.\n got: %q (len %d)\nwant: %q (len %d)", i, shortString(got), len(got), shortString(tt.body), len(tt.body))
  468. }
  469. wantLen := int64(len(tt.body))
  470. if tt.noContentLen && tt.body != "" {
  471. wantLen = -1
  472. }
  473. if ri.req.ContentLength != wantLen {
  474. t.Errorf("#%d. handler got ContentLength = %v; want %v", i, ri.req.ContentLength, wantLen)
  475. }
  476. }
  477. }
  478. func shortString(v string) string {
  479. const maxLen = 100
  480. if len(v) <= maxLen {
  481. return v
  482. }
  483. return fmt.Sprintf("%v[...%d bytes omitted...]%v", v[:maxLen/2], len(v)-maxLen, v[len(v)-maxLen/2:])
  484. }
  485. func TestTransportDialTLS(t *testing.T) {
  486. var mu sync.Mutex // guards following
  487. var gotReq, didDial bool
  488. ts := newServerTester(t,
  489. func(w http.ResponseWriter, r *http.Request) {
  490. mu.Lock()
  491. gotReq = true
  492. mu.Unlock()
  493. },
  494. optOnlyServer,
  495. )
  496. defer ts.Close()
  497. tr := &Transport{
  498. DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) {
  499. mu.Lock()
  500. didDial = true
  501. mu.Unlock()
  502. cfg.InsecureSkipVerify = true
  503. c, err := tls.Dial(netw, addr, cfg)
  504. if err != nil {
  505. return nil, err
  506. }
  507. return c, c.Handshake()
  508. },
  509. }
  510. defer tr.CloseIdleConnections()
  511. client := &http.Client{Transport: tr}
  512. res, err := client.Get(ts.ts.URL)
  513. if err != nil {
  514. t.Fatal(err)
  515. }
  516. res.Body.Close()
  517. mu.Lock()
  518. if !gotReq {
  519. t.Error("didn't get request")
  520. }
  521. if !didDial {
  522. t.Error("didn't use dial hook")
  523. }
  524. }
  525. func TestConfigureTransport(t *testing.T) {
  526. t1 := &http.Transport{}
  527. err := ConfigureTransport(t1)
  528. if err != nil {
  529. t.Fatal(err)
  530. }
  531. if got := fmt.Sprintf("%#v", t1); !strings.Contains(got, `"h2"`) {
  532. // Laziness, to avoid buildtags.
  533. t.Errorf("stringification of HTTP/1 transport didn't contain \"h2\": %v", got)
  534. }
  535. wantNextProtos := []string{"h2", "http/1.1"}
  536. if t1.TLSClientConfig == nil {
  537. t.Errorf("nil t1.TLSClientConfig")
  538. } else if !reflect.DeepEqual(t1.TLSClientConfig.NextProtos, wantNextProtos) {
  539. t.Errorf("TLSClientConfig.NextProtos = %q; want %q", t1.TLSClientConfig.NextProtos, wantNextProtos)
  540. }
  541. if err := ConfigureTransport(t1); err == nil {
  542. t.Error("unexpected success on second call to ConfigureTransport")
  543. }
  544. // And does it work?
  545. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  546. io.WriteString(w, r.Proto)
  547. }, optOnlyServer)
  548. defer st.Close()
  549. t1.TLSClientConfig.InsecureSkipVerify = true
  550. c := &http.Client{Transport: t1}
  551. res, err := c.Get(st.ts.URL)
  552. if err != nil {
  553. t.Fatal(err)
  554. }
  555. slurp, err := ioutil.ReadAll(res.Body)
  556. if err != nil {
  557. t.Fatal(err)
  558. }
  559. if got, want := string(slurp), "HTTP/2.0"; got != want {
  560. t.Errorf("body = %q; want %q", got, want)
  561. }
  562. }
  563. type capitalizeReader struct {
  564. r io.Reader
  565. }
  566. func (cr capitalizeReader) Read(p []byte) (n int, err error) {
  567. n, err = cr.r.Read(p)
  568. for i, b := range p[:n] {
  569. if b >= 'a' && b <= 'z' {
  570. p[i] = b - ('a' - 'A')
  571. }
  572. }
  573. return
  574. }
  575. type flushWriter struct {
  576. w io.Writer
  577. }
  578. func (fw flushWriter) Write(p []byte) (n int, err error) {
  579. n, err = fw.w.Write(p)
  580. if f, ok := fw.w.(http.Flusher); ok {
  581. f.Flush()
  582. }
  583. return
  584. }
  585. type clientTester struct {
  586. t *testing.T
  587. tr *Transport
  588. sc, cc net.Conn // server and client conn
  589. fr *Framer // server's framer
  590. client func() error
  591. server func() error
  592. }
  593. func newClientTester(t *testing.T) *clientTester {
  594. var dialOnce struct {
  595. sync.Mutex
  596. dialed bool
  597. }
  598. ct := &clientTester{
  599. t: t,
  600. }
  601. ct.tr = &Transport{
  602. TLSClientConfig: tlsConfigInsecure,
  603. DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
  604. dialOnce.Lock()
  605. defer dialOnce.Unlock()
  606. if dialOnce.dialed {
  607. return nil, errors.New("only one dial allowed in test mode")
  608. }
  609. dialOnce.dialed = true
  610. return ct.cc, nil
  611. },
  612. }
  613. ln := newLocalListener(t)
  614. cc, err := net.Dial("tcp", ln.Addr().String())
  615. if err != nil {
  616. t.Fatal(err)
  617. }
  618. sc, err := ln.Accept()
  619. if err != nil {
  620. t.Fatal(err)
  621. }
  622. ln.Close()
  623. ct.cc = cc
  624. ct.sc = sc
  625. ct.fr = NewFramer(sc, sc)
  626. return ct
  627. }
  628. func newLocalListener(t *testing.T) net.Listener {
  629. ln, err := net.Listen("tcp4", "127.0.0.1:0")
  630. if err == nil {
  631. return ln
  632. }
  633. ln, err = net.Listen("tcp6", "[::1]:0")
  634. if err != nil {
  635. t.Fatal(err)
  636. }
  637. return ln
  638. }
  639. func (ct *clientTester) greet(settings ...Setting) {
  640. buf := make([]byte, len(ClientPreface))
  641. _, err := io.ReadFull(ct.sc, buf)
  642. if err != nil {
  643. ct.t.Fatalf("reading client preface: %v", err)
  644. }
  645. f, err := ct.fr.ReadFrame()
  646. if err != nil {
  647. ct.t.Fatalf("Reading client settings frame: %v", err)
  648. }
  649. if sf, ok := f.(*SettingsFrame); !ok {
  650. ct.t.Fatalf("Wanted client settings frame; got %v", f)
  651. _ = sf // stash it away?
  652. }
  653. if err := ct.fr.WriteSettings(settings...); err != nil {
  654. ct.t.Fatal(err)
  655. }
  656. if err := ct.fr.WriteSettingsAck(); err != nil {
  657. ct.t.Fatal(err)
  658. }
  659. }
  660. func (ct *clientTester) readNonSettingsFrame() (Frame, error) {
  661. for {
  662. f, err := ct.fr.ReadFrame()
  663. if err != nil {
  664. return nil, err
  665. }
  666. if _, ok := f.(*SettingsFrame); ok {
  667. continue
  668. }
  669. return f, nil
  670. }
  671. }
  672. func (ct *clientTester) cleanup() {
  673. ct.tr.CloseIdleConnections()
  674. }
  675. func (ct *clientTester) run() {
  676. errc := make(chan error, 2)
  677. ct.start("client", errc, ct.client)
  678. ct.start("server", errc, ct.server)
  679. defer ct.cleanup()
  680. for i := 0; i < 2; i++ {
  681. if err := <-errc; err != nil {
  682. ct.t.Error(err)
  683. return
  684. }
  685. }
  686. }
  687. func (ct *clientTester) start(which string, errc chan<- error, fn func() error) {
  688. go func() {
  689. finished := false
  690. var err error
  691. defer func() {
  692. if !finished {
  693. err = fmt.Errorf("%s goroutine didn't finish.", which)
  694. } else if err != nil {
  695. err = fmt.Errorf("%s: %v", which, err)
  696. }
  697. errc <- err
  698. }()
  699. err = fn()
  700. finished = true
  701. }()
  702. }
  703. func (ct *clientTester) readFrame() (Frame, error) {
  704. return readFrameTimeout(ct.fr, 2*time.Second)
  705. }
  706. func (ct *clientTester) firstHeaders() (*HeadersFrame, error) {
  707. for {
  708. f, err := ct.readFrame()
  709. if err != nil {
  710. return nil, fmt.Errorf("ReadFrame while waiting for Headers: %v", err)
  711. }
  712. switch f.(type) {
  713. case *WindowUpdateFrame, *SettingsFrame:
  714. continue
  715. }
  716. hf, ok := f.(*HeadersFrame)
  717. if !ok {
  718. return nil, fmt.Errorf("Got %T; want HeadersFrame", f)
  719. }
  720. return hf, nil
  721. }
  722. }
  723. type countingReader struct {
  724. n *int64
  725. }
  726. func (r countingReader) Read(p []byte) (n int, err error) {
  727. for i := range p {
  728. p[i] = byte(i)
  729. }
  730. atomic.AddInt64(r.n, int64(len(p)))
  731. return len(p), err
  732. }
  733. func TestTransportReqBodyAfterResponse_200(t *testing.T) { testTransportReqBodyAfterResponse(t, 200) }
  734. func TestTransportReqBodyAfterResponse_403(t *testing.T) { testTransportReqBodyAfterResponse(t, 403) }
  735. func testTransportReqBodyAfterResponse(t *testing.T, status int) {
  736. const bodySize = 10 << 20
  737. clientDone := make(chan struct{})
  738. ct := newClientTester(t)
  739. ct.client = func() error {
  740. defer ct.cc.(*net.TCPConn).CloseWrite()
  741. defer close(clientDone)
  742. var n int64 // atomic
  743. req, err := http.NewRequest("PUT", "https://dummy.tld/", io.LimitReader(countingReader{&n}, bodySize))
  744. if err != nil {
  745. return err
  746. }
  747. res, err := ct.tr.RoundTrip(req)
  748. if err != nil {
  749. return fmt.Errorf("RoundTrip: %v", err)
  750. }
  751. defer res.Body.Close()
  752. if res.StatusCode != status {
  753. return fmt.Errorf("status code = %v; want %v", res.StatusCode, status)
  754. }
  755. slurp, err := ioutil.ReadAll(res.Body)
  756. if err != nil {
  757. return fmt.Errorf("Slurp: %v", err)
  758. }
  759. if len(slurp) > 0 {
  760. return fmt.Errorf("unexpected body: %q", slurp)
  761. }
  762. if status == 200 {
  763. if got := atomic.LoadInt64(&n); got != bodySize {
  764. return fmt.Errorf("For 200 response, Transport wrote %d bytes; want %d", got, bodySize)
  765. }
  766. } else {
  767. if got := atomic.LoadInt64(&n); got == 0 || got >= bodySize {
  768. return fmt.Errorf("For %d response, Transport wrote %d bytes; want (0,%d) exclusive", status, got, bodySize)
  769. }
  770. }
  771. return nil
  772. }
  773. ct.server = func() error {
  774. ct.greet()
  775. var buf bytes.Buffer
  776. enc := hpack.NewEncoder(&buf)
  777. var dataRecv int64
  778. var closed bool
  779. for {
  780. f, err := ct.fr.ReadFrame()
  781. if err != nil {
  782. select {
  783. case <-clientDone:
  784. // If the client's done, it
  785. // will have reported any
  786. // errors on its side.
  787. return nil
  788. default:
  789. return err
  790. }
  791. }
  792. //println(fmt.Sprintf("server got frame: %v", f))
  793. switch f := f.(type) {
  794. case *WindowUpdateFrame, *SettingsFrame:
  795. case *HeadersFrame:
  796. if !f.HeadersEnded() {
  797. return fmt.Errorf("headers should have END_HEADERS be ended: %v", f)
  798. }
  799. if f.StreamEnded() {
  800. return fmt.Errorf("headers contains END_STREAM unexpectedly: %v", f)
  801. }
  802. case *DataFrame:
  803. dataLen := len(f.Data())
  804. if dataLen > 0 {
  805. if dataRecv == 0 {
  806. enc.WriteField(hpack.HeaderField{Name: ":status", Value: strconv.Itoa(status)})
  807. ct.fr.WriteHeaders(HeadersFrameParam{
  808. StreamID: f.StreamID,
  809. EndHeaders: true,
  810. EndStream: false,
  811. BlockFragment: buf.Bytes(),
  812. })
  813. }
  814. if err := ct.fr.WriteWindowUpdate(0, uint32(dataLen)); err != nil {
  815. return err
  816. }
  817. if err := ct.fr.WriteWindowUpdate(f.StreamID, uint32(dataLen)); err != nil {
  818. return err
  819. }
  820. }
  821. dataRecv += int64(dataLen)
  822. if !closed && ((status != 200 && dataRecv > 0) ||
  823. (status == 200 && dataRecv == bodySize)) {
  824. closed = true
  825. if err := ct.fr.WriteData(f.StreamID, true, nil); err != nil {
  826. return err
  827. }
  828. }
  829. default:
  830. return fmt.Errorf("Unexpected client frame %v", f)
  831. }
  832. }
  833. }
  834. ct.run()
  835. }
  836. // See golang.org/issue/13444
  837. func TestTransportFullDuplex(t *testing.T) {
  838. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  839. w.WriteHeader(200) // redundant but for clarity
  840. w.(http.Flusher).Flush()
  841. io.Copy(flushWriter{w}, capitalizeReader{r.Body})
  842. fmt.Fprintf(w, "bye.\n")
  843. }, optOnlyServer)
  844. defer st.Close()
  845. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  846. defer tr.CloseIdleConnections()
  847. c := &http.Client{Transport: tr}
  848. pr, pw := io.Pipe()
  849. req, err := http.NewRequest("PUT", st.ts.URL, ioutil.NopCloser(pr))
  850. if err != nil {
  851. t.Fatal(err)
  852. }
  853. req.ContentLength = -1
  854. res, err := c.Do(req)
  855. if err != nil {
  856. t.Fatal(err)
  857. }
  858. defer res.Body.Close()
  859. if res.StatusCode != 200 {
  860. t.Fatalf("StatusCode = %v; want %v", res.StatusCode, 200)
  861. }
  862. bs := bufio.NewScanner(res.Body)
  863. want := func(v string) {
  864. if !bs.Scan() {
  865. t.Fatalf("wanted to read %q but Scan() = false, err = %v", v, bs.Err())
  866. }
  867. }
  868. write := func(v string) {
  869. _, err := io.WriteString(pw, v)
  870. if err != nil {
  871. t.Fatalf("pipe write: %v", err)
  872. }
  873. }
  874. write("foo\n")
  875. want("FOO")
  876. write("bar\n")
  877. want("BAR")
  878. pw.Close()
  879. want("bye.")
  880. if err := bs.Err(); err != nil {
  881. t.Fatal(err)
  882. }
  883. }
  884. func TestTransportConnectRequest(t *testing.T) {
  885. gotc := make(chan *http.Request, 1)
  886. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  887. gotc <- r
  888. }, optOnlyServer)
  889. defer st.Close()
  890. u, err := url.Parse(st.ts.URL)
  891. if err != nil {
  892. t.Fatal(err)
  893. }
  894. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  895. defer tr.CloseIdleConnections()
  896. c := &http.Client{Transport: tr}
  897. tests := []struct {
  898. req *http.Request
  899. want string
  900. }{
  901. {
  902. req: &http.Request{
  903. Method: "CONNECT",
  904. Header: http.Header{},
  905. URL: u,
  906. },
  907. want: u.Host,
  908. },
  909. {
  910. req: &http.Request{
  911. Method: "CONNECT",
  912. Header: http.Header{},
  913. URL: u,
  914. Host: "example.com:123",
  915. },
  916. want: "example.com:123",
  917. },
  918. }
  919. for i, tt := range tests {
  920. res, err := c.Do(tt.req)
  921. if err != nil {
  922. t.Errorf("%d. RoundTrip = %v", i, err)
  923. continue
  924. }
  925. res.Body.Close()
  926. req := <-gotc
  927. if req.Method != "CONNECT" {
  928. t.Errorf("method = %q; want CONNECT", req.Method)
  929. }
  930. if req.Host != tt.want {
  931. t.Errorf("Host = %q; want %q", req.Host, tt.want)
  932. }
  933. if req.URL.Host != tt.want {
  934. t.Errorf("URL.Host = %q; want %q", req.URL.Host, tt.want)
  935. }
  936. }
  937. }
  938. type headerType int
  939. const (
  940. noHeader headerType = iota // omitted
  941. oneHeader
  942. splitHeader // broken into continuation on purpose
  943. )
  944. const (
  945. f0 = noHeader
  946. f1 = oneHeader
  947. f2 = splitHeader
  948. d0 = false
  949. d1 = true
  950. )
  951. // Test all 36 combinations of response frame orders:
  952. // (3 ways of 100-continue) * (2 ways of headers) * (2 ways of data) * (3 ways of trailers):func TestTransportResponsePattern_00f0(t *testing.T) { testTransportResponsePattern(h0, h1, false, h0) }
  953. // Generated by http://play.golang.org/p/SScqYKJYXd
  954. func TestTransportResPattern_c0h1d0t0(t *testing.T) { testTransportResPattern(t, f0, f1, d0, f0) }
  955. func TestTransportResPattern_c0h1d0t1(t *testing.T) { testTransportResPattern(t, f0, f1, d0, f1) }
  956. func TestTransportResPattern_c0h1d0t2(t *testing.T) { testTransportResPattern(t, f0, f1, d0, f2) }
  957. func TestTransportResPattern_c0h1d1t0(t *testing.T) { testTransportResPattern(t, f0, f1, d1, f0) }
  958. func TestTransportResPattern_c0h1d1t1(t *testing.T) { testTransportResPattern(t, f0, f1, d1, f1) }
  959. func TestTransportResPattern_c0h1d1t2(t *testing.T) { testTransportResPattern(t, f0, f1, d1, f2) }
  960. func TestTransportResPattern_c0h2d0t0(t *testing.T) { testTransportResPattern(t, f0, f2, d0, f0) }
  961. func TestTransportResPattern_c0h2d0t1(t *testing.T) { testTransportResPattern(t, f0, f2, d0, f1) }
  962. func TestTransportResPattern_c0h2d0t2(t *testing.T) { testTransportResPattern(t, f0, f2, d0, f2) }
  963. func TestTransportResPattern_c0h2d1t0(t *testing.T) { testTransportResPattern(t, f0, f2, d1, f0) }
  964. func TestTransportResPattern_c0h2d1t1(t *testing.T) { testTransportResPattern(t, f0, f2, d1, f1) }
  965. func TestTransportResPattern_c0h2d1t2(t *testing.T) { testTransportResPattern(t, f0, f2, d1, f2) }
  966. func TestTransportResPattern_c1h1d0t0(t *testing.T) { testTransportResPattern(t, f1, f1, d0, f0) }
  967. func TestTransportResPattern_c1h1d0t1(t *testing.T) { testTransportResPattern(t, f1, f1, d0, f1) }
  968. func TestTransportResPattern_c1h1d0t2(t *testing.T) { testTransportResPattern(t, f1, f1, d0, f2) }
  969. func TestTransportResPattern_c1h1d1t0(t *testing.T) { testTransportResPattern(t, f1, f1, d1, f0) }
  970. func TestTransportResPattern_c1h1d1t1(t *testing.T) { testTransportResPattern(t, f1, f1, d1, f1) }
  971. func TestTransportResPattern_c1h1d1t2(t *testing.T) { testTransportResPattern(t, f1, f1, d1, f2) }
  972. func TestTransportResPattern_c1h2d0t0(t *testing.T) { testTransportResPattern(t, f1, f2, d0, f0) }
  973. func TestTransportResPattern_c1h2d0t1(t *testing.T) { testTransportResPattern(t, f1, f2, d0, f1) }
  974. func TestTransportResPattern_c1h2d0t2(t *testing.T) { testTransportResPattern(t, f1, f2, d0, f2) }
  975. func TestTransportResPattern_c1h2d1t0(t *testing.T) { testTransportResPattern(t, f1, f2, d1, f0) }
  976. func TestTransportResPattern_c1h2d1t1(t *testing.T) { testTransportResPattern(t, f1, f2, d1, f1) }
  977. func TestTransportResPattern_c1h2d1t2(t *testing.T) { testTransportResPattern(t, f1, f2, d1, f2) }
  978. func TestTransportResPattern_c2h1d0t0(t *testing.T) { testTransportResPattern(t, f2, f1, d0, f0) }
  979. func TestTransportResPattern_c2h1d0t1(t *testing.T) { testTransportResPattern(t, f2, f1, d0, f1) }
  980. func TestTransportResPattern_c2h1d0t2(t *testing.T) { testTransportResPattern(t, f2, f1, d0, f2) }
  981. func TestTransportResPattern_c2h1d1t0(t *testing.T) { testTransportResPattern(t, f2, f1, d1, f0) }
  982. func TestTransportResPattern_c2h1d1t1(t *testing.T) { testTransportResPattern(t, f2, f1, d1, f1) }
  983. func TestTransportResPattern_c2h1d1t2(t *testing.T) { testTransportResPattern(t, f2, f1, d1, f2) }
  984. func TestTransportResPattern_c2h2d0t0(t *testing.T) { testTransportResPattern(t, f2, f2, d0, f0) }
  985. func TestTransportResPattern_c2h2d0t1(t *testing.T) { testTransportResPattern(t, f2, f2, d0, f1) }
  986. func TestTransportResPattern_c2h2d0t2(t *testing.T) { testTransportResPattern(t, f2, f2, d0, f2) }
  987. func TestTransportResPattern_c2h2d1t0(t *testing.T) { testTransportResPattern(t, f2, f2, d1, f0) }
  988. func TestTransportResPattern_c2h2d1t1(t *testing.T) { testTransportResPattern(t, f2, f2, d1, f1) }
  989. func TestTransportResPattern_c2h2d1t2(t *testing.T) { testTransportResPattern(t, f2, f2, d1, f2) }
  990. func testTransportResPattern(t *testing.T, expect100Continue, resHeader headerType, withData bool, trailers headerType) {
  991. const reqBody = "some request body"
  992. const resBody = "some response body"
  993. if resHeader == noHeader {
  994. // TODO: test 100-continue followed by immediate
  995. // server stream reset, without headers in the middle?
  996. panic("invalid combination")
  997. }
  998. ct := newClientTester(t)
  999. ct.client = func() error {
  1000. req, _ := http.NewRequest("POST", "https://dummy.tld/", strings.NewReader(reqBody))
  1001. if expect100Continue != noHeader {
  1002. req.Header.Set("Expect", "100-continue")
  1003. }
  1004. res, err := ct.tr.RoundTrip(req)
  1005. if err != nil {
  1006. return fmt.Errorf("RoundTrip: %v", err)
  1007. }
  1008. defer res.Body.Close()
  1009. if res.StatusCode != 200 {
  1010. return fmt.Errorf("status code = %v; want 200", res.StatusCode)
  1011. }
  1012. slurp, err := ioutil.ReadAll(res.Body)
  1013. if err != nil {
  1014. return fmt.Errorf("Slurp: %v", err)
  1015. }
  1016. wantBody := resBody
  1017. if !withData {
  1018. wantBody = ""
  1019. }
  1020. if string(slurp) != wantBody {
  1021. return fmt.Errorf("body = %q; want %q", slurp, wantBody)
  1022. }
  1023. if trailers == noHeader {
  1024. if len(res.Trailer) > 0 {
  1025. t.Errorf("Trailer = %v; want none", res.Trailer)
  1026. }
  1027. } else {
  1028. want := http.Header{"Some-Trailer": {"some-value"}}
  1029. if !reflect.DeepEqual(res.Trailer, want) {
  1030. t.Errorf("Trailer = %v; want %v", res.Trailer, want)
  1031. }
  1032. }
  1033. return nil
  1034. }
  1035. ct.server = func() error {
  1036. ct.greet()
  1037. var buf bytes.Buffer
  1038. enc := hpack.NewEncoder(&buf)
  1039. for {
  1040. f, err := ct.fr.ReadFrame()
  1041. if err != nil {
  1042. return err
  1043. }
  1044. endStream := false
  1045. send := func(mode headerType) {
  1046. hbf := buf.Bytes()
  1047. switch mode {
  1048. case oneHeader:
  1049. ct.fr.WriteHeaders(HeadersFrameParam{
  1050. StreamID: f.Header().StreamID,
  1051. EndHeaders: true,
  1052. EndStream: endStream,
  1053. BlockFragment: hbf,
  1054. })
  1055. case splitHeader:
  1056. if len(hbf) < 2 {
  1057. panic("too small")
  1058. }
  1059. ct.fr.WriteHeaders(HeadersFrameParam{
  1060. StreamID: f.Header().StreamID,
  1061. EndHeaders: false,
  1062. EndStream: endStream,
  1063. BlockFragment: hbf[:1],
  1064. })
  1065. ct.fr.WriteContinuation(f.Header().StreamID, true, hbf[1:])
  1066. default:
  1067. panic("bogus mode")
  1068. }
  1069. }
  1070. switch f := f.(type) {
  1071. case *WindowUpdateFrame, *SettingsFrame:
  1072. case *DataFrame:
  1073. if !f.StreamEnded() {
  1074. // No need to send flow control tokens. The test request body is tiny.
  1075. continue
  1076. }
  1077. // Response headers (1+ frames; 1 or 2 in this test, but never 0)
  1078. {
  1079. buf.Reset()
  1080. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  1081. enc.WriteField(hpack.HeaderField{Name: "x-foo", Value: "blah"})
  1082. enc.WriteField(hpack.HeaderField{Name: "x-bar", Value: "more"})
  1083. if trailers != noHeader {
  1084. enc.WriteField(hpack.HeaderField{Name: "trailer", Value: "some-trailer"})
  1085. }
  1086. endStream = withData == false && trailers == noHeader
  1087. send(resHeader)
  1088. }
  1089. if withData {
  1090. endStream = trailers == noHeader
  1091. ct.fr.WriteData(f.StreamID, endStream, []byte(resBody))
  1092. }
  1093. if trailers != noHeader {
  1094. endStream = true
  1095. buf.Reset()
  1096. enc.WriteField(hpack.HeaderField{Name: "some-trailer", Value: "some-value"})
  1097. send(trailers)
  1098. }
  1099. if endStream {
  1100. return nil
  1101. }
  1102. case *HeadersFrame:
  1103. if expect100Continue != noHeader {
  1104. buf.Reset()
  1105. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "100"})
  1106. send(expect100Continue)
  1107. }
  1108. }
  1109. }
  1110. }
  1111. ct.run()
  1112. }
  1113. // Issue 26189, Issue 17739: ignore unknown 1xx responses
  1114. func TestTransportUnknown1xx(t *testing.T) {
  1115. var buf bytes.Buffer
  1116. defer func() { got1xxFuncForTests = nil }()
  1117. got1xxFuncForTests = func(code int, header textproto.MIMEHeader) error {
  1118. fmt.Fprintf(&buf, "code=%d header=%v\n", code, header)
  1119. return nil
  1120. }
  1121. ct := newClientTester(t)
  1122. ct.client = func() error {
  1123. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  1124. res, err := ct.tr.RoundTrip(req)
  1125. if err != nil {
  1126. return fmt.Errorf("RoundTrip: %v", err)
  1127. }
  1128. defer res.Body.Close()
  1129. if res.StatusCode != 204 {
  1130. return fmt.Errorf("status code = %v; want 204", res.StatusCode)
  1131. }
  1132. want := `code=110 header=map[Foo-Bar:[110]]
  1133. code=111 header=map[Foo-Bar:[111]]
  1134. code=112 header=map[Foo-Bar:[112]]
  1135. code=113 header=map[Foo-Bar:[113]]
  1136. code=114 header=map[Foo-Bar:[114]]
  1137. `
  1138. if got := buf.String(); got != want {
  1139. t.Errorf("Got trace:\n%s\nWant:\n%s", got, want)
  1140. }
  1141. return nil
  1142. }
  1143. ct.server = func() error {
  1144. ct.greet()
  1145. var buf bytes.Buffer
  1146. enc := hpack.NewEncoder(&buf)
  1147. for {
  1148. f, err := ct.fr.ReadFrame()
  1149. if err != nil {
  1150. return err
  1151. }
  1152. switch f := f.(type) {
  1153. case *WindowUpdateFrame, *SettingsFrame:
  1154. case *HeadersFrame:
  1155. for i := 110; i <= 114; i++ {
  1156. buf.Reset()
  1157. enc.WriteField(hpack.HeaderField{Name: ":status", Value: fmt.Sprint(i)})
  1158. enc.WriteField(hpack.HeaderField{Name: "foo-bar", Value: fmt.Sprint(i)})
  1159. ct.fr.WriteHeaders(HeadersFrameParam{
  1160. StreamID: f.StreamID,
  1161. EndHeaders: true,
  1162. EndStream: false,
  1163. BlockFragment: buf.Bytes(),
  1164. })
  1165. }
  1166. buf.Reset()
  1167. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "204"})
  1168. ct.fr.WriteHeaders(HeadersFrameParam{
  1169. StreamID: f.StreamID,
  1170. EndHeaders: true,
  1171. EndStream: false,
  1172. BlockFragment: buf.Bytes(),
  1173. })
  1174. return nil
  1175. }
  1176. }
  1177. }
  1178. ct.run()
  1179. }
  1180. func TestTransportReceiveUndeclaredTrailer(t *testing.T) {
  1181. ct := newClientTester(t)
  1182. ct.client = func() error {
  1183. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  1184. res, err := ct.tr.RoundTrip(req)
  1185. if err != nil {
  1186. return fmt.Errorf("RoundTrip: %v", err)
  1187. }
  1188. defer res.Body.Close()
  1189. if res.StatusCode != 200 {
  1190. return fmt.Errorf("status code = %v; want 200", res.StatusCode)
  1191. }
  1192. slurp, err := ioutil.ReadAll(res.Body)
  1193. if err != nil {
  1194. return fmt.Errorf("res.Body ReadAll error = %q, %v; want %v", slurp, err, nil)
  1195. }
  1196. if len(slurp) > 0 {
  1197. return fmt.Errorf("body = %q; want nothing", slurp)
  1198. }
  1199. if _, ok := res.Trailer["Some-Trailer"]; !ok {
  1200. return fmt.Errorf("expected Some-Trailer")
  1201. }
  1202. return nil
  1203. }
  1204. ct.server = func() error {
  1205. ct.greet()
  1206. var n int
  1207. var hf *HeadersFrame
  1208. for hf == nil && n < 10 {
  1209. f, err := ct.fr.ReadFrame()
  1210. if err != nil {
  1211. return err
  1212. }
  1213. hf, _ = f.(*HeadersFrame)
  1214. n++
  1215. }
  1216. var buf bytes.Buffer
  1217. enc := hpack.NewEncoder(&buf)
  1218. // send headers without Trailer header
  1219. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  1220. ct.fr.WriteHeaders(HeadersFrameParam{
  1221. StreamID: hf.StreamID,
  1222. EndHeaders: true,
  1223. EndStream: false,
  1224. BlockFragment: buf.Bytes(),
  1225. })
  1226. // send trailers
  1227. buf.Reset()
  1228. enc.WriteField(hpack.HeaderField{Name: "some-trailer", Value: "I'm an undeclared Trailer!"})
  1229. ct.fr.WriteHeaders(HeadersFrameParam{
  1230. StreamID: hf.StreamID,
  1231. EndHeaders: true,
  1232. EndStream: true,
  1233. BlockFragment: buf.Bytes(),
  1234. })
  1235. return nil
  1236. }
  1237. ct.run()
  1238. }
  1239. func TestTransportInvalidTrailer_Pseudo1(t *testing.T) {
  1240. testTransportInvalidTrailer_Pseudo(t, oneHeader)
  1241. }
  1242. func TestTransportInvalidTrailer_Pseudo2(t *testing.T) {
  1243. testTransportInvalidTrailer_Pseudo(t, splitHeader)
  1244. }
  1245. func testTransportInvalidTrailer_Pseudo(t *testing.T, trailers headerType) {
  1246. testInvalidTrailer(t, trailers, pseudoHeaderError(":colon"), func(enc *hpack.Encoder) {
  1247. enc.WriteField(hpack.HeaderField{Name: ":colon", Value: "foo"})
  1248. enc.WriteField(hpack.HeaderField{Name: "foo", Value: "bar"})
  1249. })
  1250. }
  1251. func TestTransportInvalidTrailer_Capital1(t *testing.T) {
  1252. testTransportInvalidTrailer_Capital(t, oneHeader)
  1253. }
  1254. func TestTransportInvalidTrailer_Capital2(t *testing.T) {
  1255. testTransportInvalidTrailer_Capital(t, splitHeader)
  1256. }
  1257. func testTransportInvalidTrailer_Capital(t *testing.T, trailers headerType) {
  1258. testInvalidTrailer(t, trailers, headerFieldNameError("Capital"), func(enc *hpack.Encoder) {
  1259. enc.WriteField(hpack.HeaderField{Name: "foo", Value: "bar"})
  1260. enc.WriteField(hpack.HeaderField{Name: "Capital", Value: "bad"})
  1261. })
  1262. }
  1263. func TestTransportInvalidTrailer_EmptyFieldName(t *testing.T) {
  1264. testInvalidTrailer(t, oneHeader, headerFieldNameError(""), func(enc *hpack.Encoder) {
  1265. enc.WriteField(hpack.HeaderField{Name: "", Value: "bad"})
  1266. })
  1267. }
  1268. func TestTransportInvalidTrailer_BinaryFieldValue(t *testing.T) {
  1269. testInvalidTrailer(t, oneHeader, headerFieldValueError("has\nnewline"), func(enc *hpack.Encoder) {
  1270. enc.WriteField(hpack.HeaderField{Name: "x", Value: "has\nnewline"})
  1271. })
  1272. }
  1273. func testInvalidTrailer(t *testing.T, trailers headerType, wantErr error, writeTrailer func(*hpack.Encoder)) {
  1274. ct := newClientTester(t)
  1275. ct.client = func() error {
  1276. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  1277. res, err := ct.tr.RoundTrip(req)
  1278. if err != nil {
  1279. return fmt.Errorf("RoundTrip: %v", err)
  1280. }
  1281. defer res.Body.Close()
  1282. if res.StatusCode != 200 {
  1283. return fmt.Errorf("status code = %v; want 200", res.StatusCode)
  1284. }
  1285. slurp, err := ioutil.ReadAll(res.Body)
  1286. se, ok := err.(StreamError)
  1287. if !ok || se.Cause != wantErr {
  1288. return fmt.Errorf("res.Body ReadAll error = %q, %#v; want StreamError with cause %T, %#v", slurp, err, wantErr, wantErr)
  1289. }
  1290. if len(slurp) > 0 {
  1291. return fmt.Errorf("body = %q; want nothing", slurp)
  1292. }
  1293. return nil
  1294. }
  1295. ct.server = func() error {
  1296. ct.greet()
  1297. var buf bytes.Buffer
  1298. enc := hpack.NewEncoder(&buf)
  1299. for {
  1300. f, err := ct.fr.ReadFrame()
  1301. if err != nil {
  1302. return err
  1303. }
  1304. switch f := f.(type) {
  1305. case *HeadersFrame:
  1306. var endStream bool
  1307. send := func(mode headerType) {
  1308. hbf := buf.Bytes()
  1309. switch mode {
  1310. case oneHeader:
  1311. ct.fr.WriteHeaders(HeadersFrameParam{
  1312. StreamID: f.StreamID,
  1313. EndHeaders: true,
  1314. EndStream: endStream,
  1315. BlockFragment: hbf,
  1316. })
  1317. case splitHeader:
  1318. if len(hbf) < 2 {
  1319. panic("too small")
  1320. }
  1321. ct.fr.WriteHeaders(HeadersFrameParam{
  1322. StreamID: f.StreamID,
  1323. EndHeaders: false,
  1324. EndStream: endStream,
  1325. BlockFragment: hbf[:1],
  1326. })
  1327. ct.fr.WriteContinuation(f.StreamID, true, hbf[1:])
  1328. default:
  1329. panic("bogus mode")
  1330. }
  1331. }
  1332. // Response headers (1+ frames; 1 or 2 in this test, but never 0)
  1333. {
  1334. buf.Reset()
  1335. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  1336. enc.WriteField(hpack.HeaderField{Name: "trailer", Value: "declared"})
  1337. endStream = false
  1338. send(oneHeader)
  1339. }
  1340. // Trailers:
  1341. {
  1342. endStream = true
  1343. buf.Reset()
  1344. writeTrailer(enc)
  1345. send(trailers)
  1346. }
  1347. return nil
  1348. }
  1349. }
  1350. }
  1351. ct.run()
  1352. }
  1353. // headerListSize returns the HTTP2 header list size of h.
  1354. // http://httpwg.org/specs/rfc7540.html#SETTINGS_MAX_HEADER_LIST_SIZE
  1355. // http://httpwg.org/specs/rfc7540.html#MaxHeaderBlock
  1356. func headerListSize(h http.Header) (size uint32) {
  1357. for k, vv := range h {
  1358. for _, v := range vv {
  1359. hf := hpack.HeaderField{Name: k, Value: v}
  1360. size += hf.Size()
  1361. }
  1362. }
  1363. return size
  1364. }
  1365. // padHeaders adds data to an http.Header until headerListSize(h) ==
  1366. // limit. Due to the way header list sizes are calculated, padHeaders
  1367. // cannot add fewer than len("Pad-Headers") + 32 bytes to h, and will
  1368. // call t.Fatal if asked to do so. PadHeaders first reserves enough
  1369. // space for an empty "Pad-Headers" key, then adds as many copies of
  1370. // filler as possible. Any remaining bytes necessary to push the
  1371. // header list size up to limit are added to h["Pad-Headers"].
  1372. func padHeaders(t *testing.T, h http.Header, limit uint64, filler string) {
  1373. if limit > 0xffffffff {
  1374. t.Fatalf("padHeaders: refusing to pad to more than 2^32-1 bytes. limit = %v", limit)
  1375. }
  1376. hf := hpack.HeaderField{Name: "Pad-Headers", Value: ""}
  1377. minPadding := uint64(hf.Size())
  1378. size := uint64(headerListSize(h))
  1379. minlimit := size + minPadding
  1380. if limit < minlimit {
  1381. t.Fatalf("padHeaders: limit %v < %v", limit, minlimit)
  1382. }
  1383. // Use a fixed-width format for name so that fieldSize
  1384. // remains constant.
  1385. nameFmt := "Pad-Headers-%06d"
  1386. hf = hpack.HeaderField{Name: fmt.Sprintf(nameFmt, 1), Value: filler}
  1387. fieldSize := uint64(hf.Size())
  1388. // Add as many complete filler values as possible, leaving
  1389. // room for at least one empty "Pad-Headers" key.
  1390. limit = limit - minPadding
  1391. for i := 0; size+fieldSize < limit; i++ {
  1392. name := fmt.Sprintf(nameFmt, i)
  1393. h.Add(name, filler)
  1394. size += fieldSize
  1395. }
  1396. // Add enough bytes to reach limit.
  1397. remain := limit - size
  1398. lastValue := strings.Repeat("*", int(remain))
  1399. h.Add("Pad-Headers", lastValue)
  1400. }
  1401. func TestPadHeaders(t *testing.T) {
  1402. check := func(h http.Header, limit uint32, fillerLen int) {
  1403. if h == nil {
  1404. h = make(http.Header)
  1405. }
  1406. filler := strings.Repeat("f", fillerLen)
  1407. padHeaders(t, h, uint64(limit), filler)
  1408. gotSize := headerListSize(h)
  1409. if gotSize != limit {
  1410. t.Errorf("Got size = %v; want %v", gotSize, limit)
  1411. }
  1412. }
  1413. // Try all possible combinations for small fillerLen and limit.
  1414. hf := hpack.HeaderField{Name: "Pad-Headers", Value: ""}
  1415. minLimit := hf.Size()
  1416. for limit := minLimit; limit <= 128; limit++ {
  1417. for fillerLen := 0; uint32(fillerLen) <= limit; fillerLen++ {
  1418. check(nil, limit, fillerLen)
  1419. }
  1420. }
  1421. // Try a few tests with larger limits, plus cumulative
  1422. // tests. Since these tests are cumulative, tests[i+1].limit
  1423. // must be >= tests[i].limit + minLimit. See the comment on
  1424. // padHeaders for more info on why the limit arg has this
  1425. // restriction.
  1426. tests := []struct {
  1427. fillerLen int
  1428. limit uint32
  1429. }{
  1430. {
  1431. fillerLen: 64,
  1432. limit: 1024,
  1433. },
  1434. {
  1435. fillerLen: 1024,
  1436. limit: 1286,
  1437. },
  1438. {
  1439. fillerLen: 256,
  1440. limit: 2048,
  1441. },
  1442. {
  1443. fillerLen: 1024,
  1444. limit: 10 * 1024,
  1445. },
  1446. {
  1447. fillerLen: 1023,
  1448. limit: 11 * 1024,
  1449. },
  1450. }
  1451. h := make(http.Header)
  1452. for _, tc := range tests {
  1453. check(nil, tc.limit, tc.fillerLen)
  1454. check(h, tc.limit, tc.fillerLen)
  1455. }
  1456. }
  1457. func TestTransportChecksRequestHeaderListSize(t *testing.T) {
  1458. st := newServerTester(t,
  1459. func(w http.ResponseWriter, r *http.Request) {
  1460. // Consume body & force client to send
  1461. // trailers before writing response.
  1462. // ioutil.ReadAll returns non-nil err for
  1463. // requests that attempt to send greater than
  1464. // maxHeaderListSize bytes of trailers, since
  1465. // those requests generate a stream reset.
  1466. ioutil.ReadAll(r.Body)
  1467. r.Body.Close()
  1468. },
  1469. func(ts *httptest.Server) {
  1470. ts.Config.MaxHeaderBytes = 16 << 10
  1471. },
  1472. optOnlyServer,
  1473. optQuiet,
  1474. )
  1475. defer st.Close()
  1476. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  1477. defer tr.CloseIdleConnections()
  1478. checkRoundTrip := func(req *http.Request, wantErr error, desc string) {
  1479. res, err := tr.RoundTrip(req)
  1480. if err != wantErr {
  1481. if res != nil {
  1482. res.Body.Close()
  1483. }
  1484. t.Errorf("%v: RoundTrip err = %v; want %v", desc, err, wantErr)
  1485. return
  1486. }
  1487. if err == nil {
  1488. if res == nil {
  1489. t.Errorf("%v: response nil; want non-nil.", desc)
  1490. return
  1491. }
  1492. defer res.Body.Close()
  1493. if res.StatusCode != http.StatusOK {
  1494. t.Errorf("%v: response status = %v; want %v", desc, res.StatusCode, http.StatusOK)
  1495. }
  1496. return
  1497. }
  1498. if res != nil {
  1499. t.Errorf("%v: RoundTrip err = %v but response non-nil", desc, err)
  1500. }
  1501. }
  1502. headerListSizeForRequest := func(req *http.Request) (size uint64) {
  1503. contentLen := actualContentLength(req)
  1504. trailers, err := commaSeparatedTrailers(req)
  1505. if err != nil {
  1506. t.Fatalf("headerListSizeForRequest: %v", err)
  1507. }
  1508. cc := &ClientConn{peerMaxHeaderListSize: 0xffffffffffffffff}
  1509. cc.henc = hpack.NewEncoder(&cc.hbuf)
  1510. cc.mu.Lock()
  1511. hdrs, err := cc.encodeHeaders(req, true, trailers, contentLen)
  1512. cc.mu.Unlock()
  1513. if err != nil {
  1514. t.Fatalf("headerListSizeForRequest: %v", err)
  1515. }
  1516. hpackDec := hpack.NewDecoder(initialHeaderTableSize, func(hf hpack.HeaderField) {
  1517. size += uint64(hf.Size())
  1518. })
  1519. if len(hdrs) > 0 {
  1520. if _, err := hpackDec.Write(hdrs); err != nil {
  1521. t.Fatalf("headerListSizeForRequest: %v", err)
  1522. }
  1523. }
  1524. return size
  1525. }
  1526. // Create a new Request for each test, rather than reusing the
  1527. // same Request, to avoid a race when modifying req.Headers.
  1528. // See https://github.com/golang/go/issues/21316
  1529. newRequest := func() *http.Request {
  1530. // Body must be non-nil to enable writing trailers.
  1531. body := strings.NewReader("hello")
  1532. req, err := http.NewRequest("POST", st.ts.URL, body)
  1533. if err != nil {
  1534. t.Fatalf("newRequest: NewRequest: %v", err)
  1535. }
  1536. return req
  1537. }
  1538. // Make an arbitrary request to ensure we get the server's
  1539. // settings frame and initialize peerMaxHeaderListSize.
  1540. req := newRequest()
  1541. checkRoundTrip(req, nil, "Initial request")
  1542. // Get the ClientConn associated with the request and validate
  1543. // peerMaxHeaderListSize.
  1544. addr := authorityAddr(req.URL.Scheme, req.URL.Host)
  1545. cc, err := tr.connPool().GetClientConn(req, addr)
  1546. if err != nil {
  1547. t.Fatalf("GetClientConn: %v", err)
  1548. }
  1549. cc.mu.Lock()
  1550. peerSize := cc.peerMaxHeaderListSize
  1551. cc.mu.Unlock()
  1552. st.scMu.Lock()
  1553. wantSize := uint64(st.sc.maxHeaderListSize())
  1554. st.scMu.Unlock()
  1555. if peerSize != wantSize {
  1556. t.Errorf("peerMaxHeaderListSize = %v; want %v", peerSize, wantSize)
  1557. }
  1558. // Sanity check peerSize. (*serverConn) maxHeaderListSize adds
  1559. // 320 bytes of padding.
  1560. wantHeaderBytes := uint64(st.ts.Config.MaxHeaderBytes) + 320
  1561. if peerSize != wantHeaderBytes {
  1562. t.Errorf("peerMaxHeaderListSize = %v; want %v.", peerSize, wantHeaderBytes)
  1563. }
  1564. // Pad headers & trailers, but stay under peerSize.
  1565. req = newRequest()
  1566. req.Header = make(http.Header)
  1567. req.Trailer = make(http.Header)
  1568. filler := strings.Repeat("*", 1024)
  1569. padHeaders(t, req.Trailer, peerSize, filler)
  1570. // cc.encodeHeaders adds some default headers to the request,
  1571. // so we need to leave room for those.
  1572. defaultBytes := headerListSizeForRequest(req)
  1573. padHeaders(t, req.Header, peerSize-defaultBytes, filler)
  1574. checkRoundTrip(req, nil, "Headers & Trailers under limit")
  1575. // Add enough header bytes to push us over peerSize.
  1576. req = newRequest()
  1577. req.Header = make(http.Header)
  1578. padHeaders(t, req.Header, peerSize, filler)
  1579. checkRoundTrip(req, errRequestHeaderListSize, "Headers over limit")
  1580. // Push trailers over the limit.
  1581. req = newRequest()
  1582. req.Trailer = make(http.Header)
  1583. padHeaders(t, req.Trailer, peerSize+1, filler)
  1584. checkRoundTrip(req, errRequestHeaderListSize, "Trailers over limit")
  1585. // Send headers with a single large value.
  1586. req = newRequest()
  1587. filler = strings.Repeat("*", int(peerSize))
  1588. req.Header = make(http.Header)
  1589. req.Header.Set("Big", filler)
  1590. checkRoundTrip(req, errRequestHeaderListSize, "Single large header")
  1591. // Send trailers with a single large value.
  1592. req = newRequest()
  1593. req.Trailer = make(http.Header)
  1594. req.Trailer.Set("Big", filler)
  1595. checkRoundTrip(req, errRequestHeaderListSize, "Single large trailer")
  1596. }
  1597. func TestTransportChecksResponseHeaderListSize(t *testing.T) {
  1598. ct := newClientTester(t)
  1599. ct.client = func() error {
  1600. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  1601. res, err := ct.tr.RoundTrip(req)
  1602. if err != errResponseHeaderListSize {
  1603. if res != nil {
  1604. res.Body.Close()
  1605. }
  1606. size := int64(0)
  1607. for k, vv := range res.Header {
  1608. for _, v := range vv {
  1609. size += int64(len(k)) + int64(len(v)) + 32
  1610. }
  1611. }
  1612. return fmt.Errorf("RoundTrip Error = %v (and %d bytes of response headers); want errResponseHeaderListSize", err, size)
  1613. }
  1614. return nil
  1615. }
  1616. ct.server = func() error {
  1617. ct.greet()
  1618. var buf bytes.Buffer
  1619. enc := hpack.NewEncoder(&buf)
  1620. for {
  1621. f, err := ct.fr.ReadFrame()
  1622. if err != nil {
  1623. return err
  1624. }
  1625. switch f := f.(type) {
  1626. case *HeadersFrame:
  1627. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  1628. large := strings.Repeat("a", 1<<10)
  1629. for i := 0; i < 5042; i++ {
  1630. enc.WriteField(hpack.HeaderField{Name: large, Value: large})
  1631. }
  1632. if size, want := buf.Len(), 6329; size != want {
  1633. // Note: this number might change if
  1634. // our hpack implementation
  1635. // changes. That's fine. This is
  1636. // just a sanity check that our
  1637. // response can fit in a single
  1638. // header block fragment frame.
  1639. return fmt.Errorf("encoding over 10MB of duplicate keypairs took %d bytes; expected %d", size, want)
  1640. }
  1641. ct.fr.WriteHeaders(HeadersFrameParam{
  1642. StreamID: f.StreamID,
  1643. EndHeaders: true,
  1644. EndStream: true,
  1645. BlockFragment: buf.Bytes(),
  1646. })
  1647. return nil
  1648. }
  1649. }
  1650. }
  1651. ct.run()
  1652. }
  1653. // Test that the Transport returns a typed error from Response.Body.Read calls
  1654. // when the server sends an error. (here we use a panic, since that should generate
  1655. // a stream error, but others like cancel should be similar)
  1656. func TestTransportBodyReadErrorType(t *testing.T) {
  1657. doPanic := make(chan bool, 1)
  1658. st := newServerTester(t,
  1659. func(w http.ResponseWriter, r *http.Request) {
  1660. w.(http.Flusher).Flush() // force headers out
  1661. <-doPanic
  1662. panic("boom")
  1663. },
  1664. optOnlyServer,
  1665. optQuiet,
  1666. )
  1667. defer st.Close()
  1668. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  1669. defer tr.CloseIdleConnections()
  1670. c := &http.Client{Transport: tr}
  1671. res, err := c.Get(st.ts.URL)
  1672. if err != nil {
  1673. t.Fatal(err)
  1674. }
  1675. defer res.Body.Close()
  1676. doPanic <- true
  1677. buf := make([]byte, 100)
  1678. n, err := res.Body.Read(buf)
  1679. want := StreamError{StreamID: 0x1, Code: 0x2}
  1680. if !reflect.DeepEqual(want, err) {
  1681. t.Errorf("Read = %v, %#v; want error %#v", n, err, want)
  1682. }
  1683. }
  1684. // golang.org/issue/13924
  1685. // This used to fail after many iterations, especially with -race:
  1686. // go test -v -run=TestTransportDoubleCloseOnWriteError -count=500 -race
  1687. func TestTransportDoubleCloseOnWriteError(t *testing.T) {
  1688. var (
  1689. mu sync.Mutex
  1690. conn net.Conn // to close if set
  1691. )
  1692. st := newServerTester(t,
  1693. func(w http.ResponseWriter, r *http.Request) {
  1694. mu.Lock()
  1695. defer mu.Unlock()
  1696. if conn != nil {
  1697. conn.Close()
  1698. }
  1699. },
  1700. optOnlyServer,
  1701. )
  1702. defer st.Close()
  1703. tr := &Transport{
  1704. TLSClientConfig: tlsConfigInsecure,
  1705. DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
  1706. tc, err := tls.Dial(network, addr, cfg)
  1707. if err != nil {
  1708. return nil, err
  1709. }
  1710. mu.Lock()
  1711. defer mu.Unlock()
  1712. conn = tc
  1713. return tc, nil
  1714. },
  1715. }
  1716. defer tr.CloseIdleConnections()
  1717. c := &http.Client{Transport: tr}
  1718. c.Get(st.ts.URL)
  1719. }
  1720. // Test that the http1 Transport.DisableKeepAlives option is respected
  1721. // and connections are closed as soon as idle.
  1722. // See golang.org/issue/14008
  1723. func TestTransportDisableKeepAlives(t *testing.T) {
  1724. st := newServerTester(t,
  1725. func(w http.ResponseWriter, r *http.Request) {
  1726. io.WriteString(w, "hi")
  1727. },
  1728. optOnlyServer,
  1729. )
  1730. defer st.Close()
  1731. connClosed := make(chan struct{}) // closed on tls.Conn.Close
  1732. tr := &Transport{
  1733. t1: &http.Transport{
  1734. DisableKeepAlives: true,
  1735. },
  1736. TLSClientConfig: tlsConfigInsecure,
  1737. DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
  1738. tc, err := tls.Dial(network, addr, cfg)
  1739. if err != nil {
  1740. return nil, err
  1741. }
  1742. return &noteCloseConn{Conn: tc, closefn: func() { close(connClosed) }}, nil
  1743. },
  1744. }
  1745. c := &http.Client{Transport: tr}
  1746. res, err := c.Get(st.ts.URL)
  1747. if err != nil {
  1748. t.Fatal(err)
  1749. }
  1750. if _, err := ioutil.ReadAll(res.Body); err != nil {
  1751. t.Fatal(err)
  1752. }
  1753. defer res.Body.Close()
  1754. select {
  1755. case <-connClosed:
  1756. case <-time.After(1 * time.Second):
  1757. t.Errorf("timeout")
  1758. }
  1759. }
  1760. // Test concurrent requests with Transport.DisableKeepAlives. We can share connections,
  1761. // but when things are totally idle, it still needs to close.
  1762. func TestTransportDisableKeepAlives_Concurrency(t *testing.T) {
  1763. const D = 25 * time.Millisecond
  1764. st := newServerTester(t,
  1765. func(w http.ResponseWriter, r *http.Request) {
  1766. time.Sleep(D)
  1767. io.WriteString(w, "hi")
  1768. },
  1769. optOnlyServer,
  1770. )
  1771. defer st.Close()
  1772. var dials int32
  1773. var conns sync.WaitGroup
  1774. tr := &Transport{
  1775. t1: &http.Transport{
  1776. DisableKeepAlives: true,
  1777. },
  1778. TLSClientConfig: tlsConfigInsecure,
  1779. DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
  1780. tc, err := tls.Dial(network, addr, cfg)
  1781. if err != nil {
  1782. return nil, err
  1783. }
  1784. atomic.AddInt32(&dials, 1)
  1785. conns.Add(1)
  1786. return &noteCloseConn{Conn: tc, closefn: func() { conns.Done() }}, nil
  1787. },
  1788. }
  1789. c := &http.Client{Transport: tr}
  1790. var reqs sync.WaitGroup
  1791. const N = 20
  1792. for i := 0; i < N; i++ {
  1793. reqs.Add(1)
  1794. if i == N-1 {
  1795. // For the final request, try to make all the
  1796. // others close. This isn't verified in the
  1797. // count, other than the Log statement, since
  1798. // it's so timing dependent. This test is
  1799. // really to make sure we don't interrupt a
  1800. // valid request.
  1801. time.Sleep(D * 2)
  1802. }
  1803. go func() {
  1804. defer reqs.Done()
  1805. res, err := c.Get(st.ts.URL)
  1806. if err != nil {
  1807. t.Error(err)
  1808. return
  1809. }
  1810. if _, err := ioutil.ReadAll(res.Body); err != nil {
  1811. t.Error(err)
  1812. return
  1813. }
  1814. res.Body.Close()
  1815. }()
  1816. }
  1817. reqs.Wait()
  1818. conns.Wait()
  1819. t.Logf("did %d dials, %d requests", atomic.LoadInt32(&dials), N)
  1820. }
  1821. type noteCloseConn struct {
  1822. net.Conn
  1823. onceClose sync.Once
  1824. closefn func()
  1825. }
  1826. func (c *noteCloseConn) Close() error {
  1827. c.onceClose.Do(c.closefn)
  1828. return c.Conn.Close()
  1829. }
  1830. func isTimeout(err error) bool {
  1831. switch err := err.(type) {
  1832. case nil:
  1833. return false
  1834. case *url.Error:
  1835. return isTimeout(err.Err)
  1836. case net.Error:
  1837. return err.Timeout()
  1838. }
  1839. return false
  1840. }
  1841. // Test that the http1 Transport.ResponseHeaderTimeout option and cancel is sent.
  1842. func TestTransportResponseHeaderTimeout_NoBody(t *testing.T) {
  1843. testTransportResponseHeaderTimeout(t, false)
  1844. }
  1845. func TestTransportResponseHeaderTimeout_Body(t *testing.T) {
  1846. testTransportResponseHeaderTimeout(t, true)
  1847. }
  1848. func testTransportResponseHeaderTimeout(t *testing.T, body bool) {
  1849. ct := newClientTester(t)
  1850. ct.tr.t1 = &http.Transport{
  1851. ResponseHeaderTimeout: 5 * time.Millisecond,
  1852. }
  1853. ct.client = func() error {
  1854. c := &http.Client{Transport: ct.tr}
  1855. var err error
  1856. var n int64
  1857. const bodySize = 4 << 20
  1858. if body {
  1859. _, err = c.Post("https://dummy.tld/", "text/foo", io.LimitReader(countingReader{&n}, bodySize))
  1860. } else {
  1861. _, err = c.Get("https://dummy.tld/")
  1862. }
  1863. if !isTimeout(err) {
  1864. t.Errorf("client expected timeout error; got %#v", err)
  1865. }
  1866. if body && n != bodySize {
  1867. t.Errorf("only read %d bytes of body; want %d", n, bodySize)
  1868. }
  1869. return nil
  1870. }
  1871. ct.server = func() error {
  1872. ct.greet()
  1873. for {
  1874. f, err := ct.fr.ReadFrame()
  1875. if err != nil {
  1876. t.Logf("ReadFrame: %v", err)
  1877. return nil
  1878. }
  1879. switch f := f.(type) {
  1880. case *DataFrame:
  1881. dataLen := len(f.Data())
  1882. if dataLen > 0 {
  1883. if err := ct.fr.WriteWindowUpdate(0, uint32(dataLen)); err != nil {
  1884. return err
  1885. }
  1886. if err := ct.fr.WriteWindowUpdate(f.StreamID, uint32(dataLen)); err != nil {
  1887. return err
  1888. }
  1889. }
  1890. case *RSTStreamFrame:
  1891. if f.StreamID == 1 && f.ErrCode == ErrCodeCancel {
  1892. return nil
  1893. }
  1894. }
  1895. }
  1896. }
  1897. ct.run()
  1898. }
  1899. func TestTransportDisableCompression(t *testing.T) {
  1900. const body = "sup"
  1901. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  1902. want := http.Header{
  1903. "User-Agent": []string{"Go-http-client/2.0"},
  1904. }
  1905. if !reflect.DeepEqual(r.Header, want) {
  1906. t.Errorf("request headers = %v; want %v", r.Header, want)
  1907. }
  1908. }, optOnlyServer)
  1909. defer st.Close()
  1910. tr := &Transport{
  1911. TLSClientConfig: tlsConfigInsecure,
  1912. t1: &http.Transport{
  1913. DisableCompression: true,
  1914. },
  1915. }
  1916. defer tr.CloseIdleConnections()
  1917. req, err := http.NewRequest("GET", st.ts.URL, nil)
  1918. if err != nil {
  1919. t.Fatal(err)
  1920. }
  1921. res, err := tr.RoundTrip(req)
  1922. if err != nil {
  1923. t.Fatal(err)
  1924. }
  1925. defer res.Body.Close()
  1926. }
  1927. // RFC 7540 section 8.1.2.2
  1928. func TestTransportRejectsConnHeaders(t *testing.T) {
  1929. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  1930. var got []string
  1931. for k := range r.Header {
  1932. got = append(got, k)
  1933. }
  1934. sort.Strings(got)
  1935. w.Header().Set("Got-Header", strings.Join(got, ","))
  1936. }, optOnlyServer)
  1937. defer st.Close()
  1938. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  1939. defer tr.CloseIdleConnections()
  1940. tests := []struct {
  1941. key string
  1942. value []string
  1943. want string
  1944. }{
  1945. {
  1946. key: "Upgrade",
  1947. value: []string{"anything"},
  1948. want: "ERROR: http2: invalid Upgrade request header: [\"anything\"]",
  1949. },
  1950. {
  1951. key: "Connection",
  1952. value: []string{"foo"},
  1953. want: "ERROR: http2: invalid Connection request header: [\"foo\"]",
  1954. },
  1955. {
  1956. key: "Connection",
  1957. value: []string{"close"},
  1958. want: "Accept-Encoding,User-Agent",
  1959. },
  1960. {
  1961. key: "Connection",
  1962. value: []string{"CLoSe"},
  1963. want: "Accept-Encoding,User-Agent",
  1964. },
  1965. {
  1966. key: "Connection",
  1967. value: []string{"close", "something-else"},
  1968. want: "ERROR: http2: invalid Connection request header: [\"close\" \"something-else\"]",
  1969. },
  1970. {
  1971. key: "Connection",
  1972. value: []string{"keep-alive"},
  1973. want: "Accept-Encoding,User-Agent",
  1974. },
  1975. {
  1976. key: "Connection",
  1977. value: []string{"Keep-ALIVE"},
  1978. want: "Accept-Encoding,User-Agent",
  1979. },
  1980. {
  1981. key: "Proxy-Connection", // just deleted and ignored
  1982. value: []string{"keep-alive"},
  1983. want: "Accept-Encoding,User-Agent",
  1984. },
  1985. {
  1986. key: "Transfer-Encoding",
  1987. value: []string{""},
  1988. want: "Accept-Encoding,User-Agent",
  1989. },
  1990. {
  1991. key: "Transfer-Encoding",
  1992. value: []string{"foo"},
  1993. want: "ERROR: http2: invalid Transfer-Encoding request header: [\"foo\"]",
  1994. },
  1995. {
  1996. key: "Transfer-Encoding",
  1997. value: []string{"chunked"},
  1998. want: "Accept-Encoding,User-Agent",
  1999. },
  2000. {
  2001. key: "Transfer-Encoding",
  2002. value: []string{"chunked", "other"},
  2003. want: "ERROR: http2: invalid Transfer-Encoding request header: [\"chunked\" \"other\"]",
  2004. },
  2005. {
  2006. key: "Content-Length",
  2007. value: []string{"123"},
  2008. want: "Accept-Encoding,User-Agent",
  2009. },
  2010. {
  2011. key: "Keep-Alive",
  2012. value: []string{"doop"},
  2013. want: "Accept-Encoding,User-Agent",
  2014. },
  2015. }
  2016. for _, tt := range tests {
  2017. req, _ := http.NewRequest("GET", st.ts.URL, nil)
  2018. req.Header[tt.key] = tt.value
  2019. res, err := tr.RoundTrip(req)
  2020. var got string
  2021. if err != nil {
  2022. got = fmt.Sprintf("ERROR: %v", err)
  2023. } else {
  2024. got = res.Header.Get("Got-Header")
  2025. res.Body.Close()
  2026. }
  2027. if got != tt.want {
  2028. t.Errorf("For key %q, value %q, got = %q; want %q", tt.key, tt.value, got, tt.want)
  2029. }
  2030. }
  2031. }
  2032. // golang.org/issue/14048
  2033. func TestTransportFailsOnInvalidHeaders(t *testing.T) {
  2034. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  2035. var got []string
  2036. for k := range r.Header {
  2037. got = append(got, k)
  2038. }
  2039. sort.Strings(got)
  2040. w.Header().Set("Got-Header", strings.Join(got, ","))
  2041. }, optOnlyServer)
  2042. defer st.Close()
  2043. tests := [...]struct {
  2044. h http.Header
  2045. wantErr string
  2046. }{
  2047. 0: {
  2048. h: http.Header{"with space": {"foo"}},
  2049. wantErr: `invalid HTTP header name "with space"`,
  2050. },
  2051. 1: {
  2052. h: http.Header{"name": {"Брэд"}},
  2053. wantErr: "", // okay
  2054. },
  2055. 2: {
  2056. h: http.Header{"имя": {"Brad"}},
  2057. wantErr: `invalid HTTP header name "имя"`,
  2058. },
  2059. 3: {
  2060. h: http.Header{"foo": {"foo\x01bar"}},
  2061. wantErr: `invalid HTTP header value "foo\x01bar" for header "foo"`,
  2062. },
  2063. }
  2064. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  2065. defer tr.CloseIdleConnections()
  2066. for i, tt := range tests {
  2067. req, _ := http.NewRequest("GET", st.ts.URL, nil)
  2068. req.Header = tt.h
  2069. res, err := tr.RoundTrip(req)
  2070. var bad bool
  2071. if tt.wantErr == "" {
  2072. if err != nil {
  2073. bad = true
  2074. t.Errorf("case %d: error = %v; want no error", i, err)
  2075. }
  2076. } else {
  2077. if !strings.Contains(fmt.Sprint(err), tt.wantErr) {
  2078. bad = true
  2079. t.Errorf("case %d: error = %v; want error %q", i, err, tt.wantErr)
  2080. }
  2081. }
  2082. if err == nil {
  2083. if bad {
  2084. t.Logf("case %d: server got headers %q", i, res.Header.Get("Got-Header"))
  2085. }
  2086. res.Body.Close()
  2087. }
  2088. }
  2089. }
  2090. // Tests that gzipReader doesn't crash on a second Read call following
  2091. // the first Read call's gzip.NewReader returning an error.
  2092. func TestGzipReader_DoubleReadCrash(t *testing.T) {
  2093. gz := &gzipReader{
  2094. body: ioutil.NopCloser(strings.NewReader("0123456789")),
  2095. }
  2096. var buf [1]byte
  2097. n, err1 := gz.Read(buf[:])
  2098. if n != 0 || !strings.Contains(fmt.Sprint(err1), "invalid header") {
  2099. t.Fatalf("Read = %v, %v; want 0, invalid header", n, err1)
  2100. }
  2101. n, err2 := gz.Read(buf[:])
  2102. if n != 0 || err2 != err1 {
  2103. t.Fatalf("second Read = %v, %v; want 0, %v", n, err2, err1)
  2104. }
  2105. }
  2106. func TestTransportNewTLSConfig(t *testing.T) {
  2107. tests := [...]struct {
  2108. conf *tls.Config
  2109. host string
  2110. want *tls.Config
  2111. }{
  2112. // Normal case.
  2113. 0: {
  2114. conf: nil,
  2115. host: "foo.com",
  2116. want: &tls.Config{
  2117. ServerName: "foo.com",
  2118. NextProtos: []string{NextProtoTLS},
  2119. },
  2120. },
  2121. // User-provided name (bar.com) takes precedence:
  2122. 1: {
  2123. conf: &tls.Config{
  2124. ServerName: "bar.com",
  2125. },
  2126. host: "foo.com",
  2127. want: &tls.Config{
  2128. ServerName: "bar.com",
  2129. NextProtos: []string{NextProtoTLS},
  2130. },
  2131. },
  2132. // NextProto is prepended:
  2133. 2: {
  2134. conf: &tls.Config{
  2135. NextProtos: []string{"foo", "bar"},
  2136. },
  2137. host: "example.com",
  2138. want: &tls.Config{
  2139. ServerName: "example.com",
  2140. NextProtos: []string{NextProtoTLS, "foo", "bar"},
  2141. },
  2142. },
  2143. // NextProto is not duplicated:
  2144. 3: {
  2145. conf: &tls.Config{
  2146. NextProtos: []string{"foo", "bar", NextProtoTLS},
  2147. },
  2148. host: "example.com",
  2149. want: &tls.Config{
  2150. ServerName: "example.com",
  2151. NextProtos: []string{"foo", "bar", NextProtoTLS},
  2152. },
  2153. },
  2154. }
  2155. for i, tt := range tests {
  2156. // Ignore the session ticket keys part, which ends up populating
  2157. // unexported fields in the Config:
  2158. if tt.conf != nil {
  2159. tt.conf.SessionTicketsDisabled = true
  2160. }
  2161. tr := &Transport{TLSClientConfig: tt.conf}
  2162. got := tr.newTLSConfig(tt.host)
  2163. got.SessionTicketsDisabled = false
  2164. if !reflect.DeepEqual(got, tt.want) {
  2165. t.Errorf("%d. got %#v; want %#v", i, got, tt.want)
  2166. }
  2167. }
  2168. }
  2169. // The Google GFE responds to HEAD requests with a HEADERS frame
  2170. // without END_STREAM, followed by a 0-length DATA frame with
  2171. // END_STREAM. Make sure we don't get confused by that. (We did.)
  2172. func TestTransportReadHeadResponse(t *testing.T) {
  2173. ct := newClientTester(t)
  2174. clientDone := make(chan struct{})
  2175. ct.client = func() error {
  2176. defer close(clientDone)
  2177. req, _ := http.NewRequest("HEAD", "https://dummy.tld/", nil)
  2178. res, err := ct.tr.RoundTrip(req)
  2179. if err != nil {
  2180. return err
  2181. }
  2182. if res.ContentLength != 123 {
  2183. return fmt.Errorf("Content-Length = %d; want 123", res.ContentLength)
  2184. }
  2185. slurp, err := ioutil.ReadAll(res.Body)
  2186. if err != nil {
  2187. return fmt.Errorf("ReadAll: %v", err)
  2188. }
  2189. if len(slurp) > 0 {
  2190. return fmt.Errorf("Unexpected non-empty ReadAll body: %q", slurp)
  2191. }
  2192. return nil
  2193. }
  2194. ct.server = func() error {
  2195. ct.greet()
  2196. for {
  2197. f, err := ct.fr.ReadFrame()
  2198. if err != nil {
  2199. t.Logf("ReadFrame: %v", err)
  2200. return nil
  2201. }
  2202. hf, ok := f.(*HeadersFrame)
  2203. if !ok {
  2204. continue
  2205. }
  2206. var buf bytes.Buffer
  2207. enc := hpack.NewEncoder(&buf)
  2208. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2209. enc.WriteField(hpack.HeaderField{Name: "content-length", Value: "123"})
  2210. ct.fr.WriteHeaders(HeadersFrameParam{
  2211. StreamID: hf.StreamID,
  2212. EndHeaders: true,
  2213. EndStream: false, // as the GFE does
  2214. BlockFragment: buf.Bytes(),
  2215. })
  2216. ct.fr.WriteData(hf.StreamID, true, nil)
  2217. <-clientDone
  2218. return nil
  2219. }
  2220. }
  2221. ct.run()
  2222. }
  2223. func TestTransportReadHeadResponseWithBody(t *testing.T) {
  2224. // This test use not valid response format.
  2225. // Discarding logger output to not spam tests output.
  2226. log.SetOutput(ioutil.Discard)
  2227. defer log.SetOutput(os.Stderr)
  2228. response := "redirecting to /elsewhere"
  2229. ct := newClientTester(t)
  2230. clientDone := make(chan struct{})
  2231. ct.client = func() error {
  2232. defer close(clientDone)
  2233. req, _ := http.NewRequest("HEAD", "https://dummy.tld/", nil)
  2234. res, err := ct.tr.RoundTrip(req)
  2235. if err != nil {
  2236. return err
  2237. }
  2238. if res.ContentLength != int64(len(response)) {
  2239. return fmt.Errorf("Content-Length = %d; want %d", res.ContentLength, len(response))
  2240. }
  2241. slurp, err := ioutil.ReadAll(res.Body)
  2242. if err != nil {
  2243. return fmt.Errorf("ReadAll: %v", err)
  2244. }
  2245. if len(slurp) > 0 {
  2246. return fmt.Errorf("Unexpected non-empty ReadAll body: %q", slurp)
  2247. }
  2248. return nil
  2249. }
  2250. ct.server = func() error {
  2251. ct.greet()
  2252. for {
  2253. f, err := ct.fr.ReadFrame()
  2254. if err != nil {
  2255. t.Logf("ReadFrame: %v", err)
  2256. return nil
  2257. }
  2258. hf, ok := f.(*HeadersFrame)
  2259. if !ok {
  2260. continue
  2261. }
  2262. var buf bytes.Buffer
  2263. enc := hpack.NewEncoder(&buf)
  2264. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2265. enc.WriteField(hpack.HeaderField{Name: "content-length", Value: strconv.Itoa(len(response))})
  2266. ct.fr.WriteHeaders(HeadersFrameParam{
  2267. StreamID: hf.StreamID,
  2268. EndHeaders: true,
  2269. EndStream: false,
  2270. BlockFragment: buf.Bytes(),
  2271. })
  2272. ct.fr.WriteData(hf.StreamID, true, []byte(response))
  2273. <-clientDone
  2274. return nil
  2275. }
  2276. }
  2277. ct.run()
  2278. }
  2279. type neverEnding byte
  2280. func (b neverEnding) Read(p []byte) (int, error) {
  2281. for i := range p {
  2282. p[i] = byte(b)
  2283. }
  2284. return len(p), nil
  2285. }
  2286. // golang.org/issue/15425: test that a handler closing the request
  2287. // body doesn't terminate the stream to the peer. (It just stops
  2288. // readability from the handler's side, and eventually the client
  2289. // runs out of flow control tokens)
  2290. func TestTransportHandlerBodyClose(t *testing.T) {
  2291. const bodySize = 10 << 20
  2292. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  2293. r.Body.Close()
  2294. io.Copy(w, io.LimitReader(neverEnding('A'), bodySize))
  2295. }, optOnlyServer)
  2296. defer st.Close()
  2297. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  2298. defer tr.CloseIdleConnections()
  2299. g0 := runtime.NumGoroutine()
  2300. const numReq = 10
  2301. for i := 0; i < numReq; i++ {
  2302. req, err := http.NewRequest("POST", st.ts.URL, struct{ io.Reader }{io.LimitReader(neverEnding('A'), bodySize)})
  2303. if err != nil {
  2304. t.Fatal(err)
  2305. }
  2306. res, err := tr.RoundTrip(req)
  2307. if err != nil {
  2308. t.Fatal(err)
  2309. }
  2310. n, err := io.Copy(ioutil.Discard, res.Body)
  2311. res.Body.Close()
  2312. if n != bodySize || err != nil {
  2313. t.Fatalf("req#%d: Copy = %d, %v; want %d, nil", i, n, err, bodySize)
  2314. }
  2315. }
  2316. tr.CloseIdleConnections()
  2317. if !waitCondition(5*time.Second, 100*time.Millisecond, func() bool {
  2318. gd := runtime.NumGoroutine() - g0
  2319. return gd < numReq/2
  2320. }) {
  2321. t.Errorf("appeared to leak goroutines")
  2322. }
  2323. }
  2324. // https://golang.org/issue/15930
  2325. func TestTransportFlowControl(t *testing.T) {
  2326. const bufLen = 64 << 10
  2327. var total int64 = 100 << 20 // 100MB
  2328. if testing.Short() {
  2329. total = 10 << 20
  2330. }
  2331. var wrote int64 // updated atomically
  2332. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  2333. b := make([]byte, bufLen)
  2334. for wrote < total {
  2335. n, err := w.Write(b)
  2336. atomic.AddInt64(&wrote, int64(n))
  2337. if err != nil {
  2338. t.Errorf("ResponseWriter.Write error: %v", err)
  2339. break
  2340. }
  2341. w.(http.Flusher).Flush()
  2342. }
  2343. }, optOnlyServer)
  2344. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  2345. defer tr.CloseIdleConnections()
  2346. req, err := http.NewRequest("GET", st.ts.URL, nil)
  2347. if err != nil {
  2348. t.Fatal("NewRequest error:", err)
  2349. }
  2350. resp, err := tr.RoundTrip(req)
  2351. if err != nil {
  2352. t.Fatal("RoundTrip error:", err)
  2353. }
  2354. defer resp.Body.Close()
  2355. var read int64
  2356. b := make([]byte, bufLen)
  2357. for {
  2358. n, err := resp.Body.Read(b)
  2359. if err == io.EOF {
  2360. break
  2361. }
  2362. if err != nil {
  2363. t.Fatal("Read error:", err)
  2364. }
  2365. read += int64(n)
  2366. const max = transportDefaultStreamFlow
  2367. if w := atomic.LoadInt64(&wrote); -max > read-w || read-w > max {
  2368. t.Fatalf("Too much data inflight: server wrote %v bytes but client only received %v", w, read)
  2369. }
  2370. // Let the server get ahead of the client.
  2371. time.Sleep(1 * time.Millisecond)
  2372. }
  2373. }
  2374. // golang.org/issue/14627 -- if the server sends a GOAWAY frame, make
  2375. // the Transport remember it and return it back to users (via
  2376. // RoundTrip or request body reads) if needed (e.g. if the server
  2377. // proceeds to close the TCP connection before the client gets its
  2378. // response)
  2379. func TestTransportUsesGoAwayDebugError_RoundTrip(t *testing.T) {
  2380. testTransportUsesGoAwayDebugError(t, false)
  2381. }
  2382. func TestTransportUsesGoAwayDebugError_Body(t *testing.T) {
  2383. testTransportUsesGoAwayDebugError(t, true)
  2384. }
  2385. func testTransportUsesGoAwayDebugError(t *testing.T, failMidBody bool) {
  2386. ct := newClientTester(t)
  2387. clientDone := make(chan struct{})
  2388. const goAwayErrCode = ErrCodeHTTP11Required // arbitrary
  2389. const goAwayDebugData = "some debug data"
  2390. ct.client = func() error {
  2391. defer close(clientDone)
  2392. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  2393. res, err := ct.tr.RoundTrip(req)
  2394. if failMidBody {
  2395. if err != nil {
  2396. return fmt.Errorf("unexpected client RoundTrip error: %v", err)
  2397. }
  2398. _, err = io.Copy(ioutil.Discard, res.Body)
  2399. res.Body.Close()
  2400. }
  2401. want := GoAwayError{
  2402. LastStreamID: 5,
  2403. ErrCode: goAwayErrCode,
  2404. DebugData: goAwayDebugData,
  2405. }
  2406. if !reflect.DeepEqual(err, want) {
  2407. t.Errorf("RoundTrip error = %T: %#v, want %T (%#v)", err, err, want, want)
  2408. }
  2409. return nil
  2410. }
  2411. ct.server = func() error {
  2412. ct.greet()
  2413. for {
  2414. f, err := ct.fr.ReadFrame()
  2415. if err != nil {
  2416. t.Logf("ReadFrame: %v", err)
  2417. return nil
  2418. }
  2419. hf, ok := f.(*HeadersFrame)
  2420. if !ok {
  2421. continue
  2422. }
  2423. if failMidBody {
  2424. var buf bytes.Buffer
  2425. enc := hpack.NewEncoder(&buf)
  2426. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2427. enc.WriteField(hpack.HeaderField{Name: "content-length", Value: "123"})
  2428. ct.fr.WriteHeaders(HeadersFrameParam{
  2429. StreamID: hf.StreamID,
  2430. EndHeaders: true,
  2431. EndStream: false,
  2432. BlockFragment: buf.Bytes(),
  2433. })
  2434. }
  2435. // Write two GOAWAY frames, to test that the Transport takes
  2436. // the interesting parts of both.
  2437. ct.fr.WriteGoAway(5, ErrCodeNo, []byte(goAwayDebugData))
  2438. ct.fr.WriteGoAway(5, goAwayErrCode, nil)
  2439. ct.sc.(*net.TCPConn).CloseWrite()
  2440. <-clientDone
  2441. return nil
  2442. }
  2443. }
  2444. ct.run()
  2445. }
  2446. func testTransportReturnsUnusedFlowControl(t *testing.T, oneDataFrame bool) {
  2447. ct := newClientTester(t)
  2448. clientClosed := make(chan struct{})
  2449. serverWroteFirstByte := make(chan struct{})
  2450. ct.client = func() error {
  2451. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  2452. res, err := ct.tr.RoundTrip(req)
  2453. if err != nil {
  2454. return err
  2455. }
  2456. <-serverWroteFirstByte
  2457. if n, err := res.Body.Read(make([]byte, 1)); err != nil || n != 1 {
  2458. return fmt.Errorf("body read = %v, %v; want 1, nil", n, err)
  2459. }
  2460. res.Body.Close() // leaving 4999 bytes unread
  2461. close(clientClosed)
  2462. return nil
  2463. }
  2464. ct.server = func() error {
  2465. ct.greet()
  2466. var hf *HeadersFrame
  2467. for {
  2468. f, err := ct.fr.ReadFrame()
  2469. if err != nil {
  2470. return fmt.Errorf("ReadFrame while waiting for Headers: %v", err)
  2471. }
  2472. switch f.(type) {
  2473. case *WindowUpdateFrame, *SettingsFrame:
  2474. continue
  2475. }
  2476. var ok bool
  2477. hf, ok = f.(*HeadersFrame)
  2478. if !ok {
  2479. return fmt.Errorf("Got %T; want HeadersFrame", f)
  2480. }
  2481. break
  2482. }
  2483. var buf bytes.Buffer
  2484. enc := hpack.NewEncoder(&buf)
  2485. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2486. enc.WriteField(hpack.HeaderField{Name: "content-length", Value: "5000"})
  2487. ct.fr.WriteHeaders(HeadersFrameParam{
  2488. StreamID: hf.StreamID,
  2489. EndHeaders: true,
  2490. EndStream: false,
  2491. BlockFragment: buf.Bytes(),
  2492. })
  2493. // Two cases:
  2494. // - Send one DATA frame with 5000 bytes.
  2495. // - Send two DATA frames with 1 and 4999 bytes each.
  2496. //
  2497. // In both cases, the client should consume one byte of data,
  2498. // refund that byte, then refund the following 4999 bytes.
  2499. //
  2500. // In the second case, the server waits for the client connection to
  2501. // close before seconding the second DATA frame. This tests the case
  2502. // where the client receives a DATA frame after it has reset the stream.
  2503. if oneDataFrame {
  2504. ct.fr.WriteData(hf.StreamID, false /* don't end stream */, make([]byte, 5000))
  2505. close(serverWroteFirstByte)
  2506. <-clientClosed
  2507. } else {
  2508. ct.fr.WriteData(hf.StreamID, false /* don't end stream */, make([]byte, 1))
  2509. close(serverWroteFirstByte)
  2510. <-clientClosed
  2511. ct.fr.WriteData(hf.StreamID, false /* don't end stream */, make([]byte, 4999))
  2512. }
  2513. waitingFor := "RSTStreamFrame"
  2514. for {
  2515. f, err := ct.fr.ReadFrame()
  2516. if err != nil {
  2517. return fmt.Errorf("ReadFrame while waiting for %s: %v", waitingFor, err)
  2518. }
  2519. if _, ok := f.(*SettingsFrame); ok {
  2520. continue
  2521. }
  2522. switch waitingFor {
  2523. case "RSTStreamFrame":
  2524. if rf, ok := f.(*RSTStreamFrame); !ok || rf.ErrCode != ErrCodeCancel {
  2525. return fmt.Errorf("Expected a RSTStreamFrame with code cancel; got %v", summarizeFrame(f))
  2526. }
  2527. waitingFor = "WindowUpdateFrame"
  2528. case "WindowUpdateFrame":
  2529. if wuf, ok := f.(*WindowUpdateFrame); !ok || wuf.Increment != 4999 {
  2530. return fmt.Errorf("Expected WindowUpdateFrame for 4999 bytes; got %v", summarizeFrame(f))
  2531. }
  2532. return nil
  2533. }
  2534. }
  2535. }
  2536. ct.run()
  2537. }
  2538. // See golang.org/issue/16481
  2539. func TestTransportReturnsUnusedFlowControlSingleWrite(t *testing.T) {
  2540. testTransportReturnsUnusedFlowControl(t, true)
  2541. }
  2542. // See golang.org/issue/20469
  2543. func TestTransportReturnsUnusedFlowControlMultipleWrites(t *testing.T) {
  2544. testTransportReturnsUnusedFlowControl(t, false)
  2545. }
  2546. // Issue 16612: adjust flow control on open streams when transport
  2547. // receives SETTINGS with INITIAL_WINDOW_SIZE from server.
  2548. func TestTransportAdjustsFlowControl(t *testing.T) {
  2549. ct := newClientTester(t)
  2550. clientDone := make(chan struct{})
  2551. const bodySize = 1 << 20
  2552. ct.client = func() error {
  2553. defer ct.cc.(*net.TCPConn).CloseWrite()
  2554. defer close(clientDone)
  2555. req, _ := http.NewRequest("POST", "https://dummy.tld/", struct{ io.Reader }{io.LimitReader(neverEnding('A'), bodySize)})
  2556. res, err := ct.tr.RoundTrip(req)
  2557. if err != nil {
  2558. return err
  2559. }
  2560. res.Body.Close()
  2561. return nil
  2562. }
  2563. ct.server = func() error {
  2564. _, err := io.ReadFull(ct.sc, make([]byte, len(ClientPreface)))
  2565. if err != nil {
  2566. return fmt.Errorf("reading client preface: %v", err)
  2567. }
  2568. var gotBytes int64
  2569. var sentSettings bool
  2570. for {
  2571. f, err := ct.fr.ReadFrame()
  2572. if err != nil {
  2573. select {
  2574. case <-clientDone:
  2575. return nil
  2576. default:
  2577. return fmt.Errorf("ReadFrame while waiting for Headers: %v", err)
  2578. }
  2579. }
  2580. switch f := f.(type) {
  2581. case *DataFrame:
  2582. gotBytes += int64(len(f.Data()))
  2583. // After we've got half the client's
  2584. // initial flow control window's worth
  2585. // of request body data, give it just
  2586. // enough flow control to finish.
  2587. if gotBytes >= initialWindowSize/2 && !sentSettings {
  2588. sentSettings = true
  2589. ct.fr.WriteSettings(Setting{ID: SettingInitialWindowSize, Val: bodySize})
  2590. ct.fr.WriteWindowUpdate(0, bodySize)
  2591. ct.fr.WriteSettingsAck()
  2592. }
  2593. if f.StreamEnded() {
  2594. var buf bytes.Buffer
  2595. enc := hpack.NewEncoder(&buf)
  2596. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2597. ct.fr.WriteHeaders(HeadersFrameParam{
  2598. StreamID: f.StreamID,
  2599. EndHeaders: true,
  2600. EndStream: true,
  2601. BlockFragment: buf.Bytes(),
  2602. })
  2603. }
  2604. }
  2605. }
  2606. }
  2607. ct.run()
  2608. }
  2609. // See golang.org/issue/16556
  2610. func TestTransportReturnsDataPaddingFlowControl(t *testing.T) {
  2611. ct := newClientTester(t)
  2612. unblockClient := make(chan bool, 1)
  2613. ct.client = func() error {
  2614. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  2615. res, err := ct.tr.RoundTrip(req)
  2616. if err != nil {
  2617. return err
  2618. }
  2619. defer res.Body.Close()
  2620. <-unblockClient
  2621. return nil
  2622. }
  2623. ct.server = func() error {
  2624. ct.greet()
  2625. var hf *HeadersFrame
  2626. for {
  2627. f, err := ct.fr.ReadFrame()
  2628. if err != nil {
  2629. return fmt.Errorf("ReadFrame while waiting for Headers: %v", err)
  2630. }
  2631. switch f.(type) {
  2632. case *WindowUpdateFrame, *SettingsFrame:
  2633. continue
  2634. }
  2635. var ok bool
  2636. hf, ok = f.(*HeadersFrame)
  2637. if !ok {
  2638. return fmt.Errorf("Got %T; want HeadersFrame", f)
  2639. }
  2640. break
  2641. }
  2642. var buf bytes.Buffer
  2643. enc := hpack.NewEncoder(&buf)
  2644. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2645. enc.WriteField(hpack.HeaderField{Name: "content-length", Value: "5000"})
  2646. ct.fr.WriteHeaders(HeadersFrameParam{
  2647. StreamID: hf.StreamID,
  2648. EndHeaders: true,
  2649. EndStream: false,
  2650. BlockFragment: buf.Bytes(),
  2651. })
  2652. pad := make([]byte, 5)
  2653. ct.fr.WriteDataPadded(hf.StreamID, false, make([]byte, 5000), pad) // without ending stream
  2654. f, err := ct.readNonSettingsFrame()
  2655. if err != nil {
  2656. return fmt.Errorf("ReadFrame while waiting for first WindowUpdateFrame: %v", err)
  2657. }
  2658. wantBack := uint32(len(pad)) + 1 // one byte for the length of the padding
  2659. if wuf, ok := f.(*WindowUpdateFrame); !ok || wuf.Increment != wantBack || wuf.StreamID != 0 {
  2660. return fmt.Errorf("Expected conn WindowUpdateFrame for %d bytes; got %v", wantBack, summarizeFrame(f))
  2661. }
  2662. f, err = ct.readNonSettingsFrame()
  2663. if err != nil {
  2664. return fmt.Errorf("ReadFrame while waiting for second WindowUpdateFrame: %v", err)
  2665. }
  2666. if wuf, ok := f.(*WindowUpdateFrame); !ok || wuf.Increment != wantBack || wuf.StreamID == 0 {
  2667. return fmt.Errorf("Expected stream WindowUpdateFrame for %d bytes; got %v", wantBack, summarizeFrame(f))
  2668. }
  2669. unblockClient <- true
  2670. return nil
  2671. }
  2672. ct.run()
  2673. }
  2674. // golang.org/issue/16572 -- RoundTrip shouldn't hang when it gets a
  2675. // StreamError as a result of the response HEADERS
  2676. func TestTransportReturnsErrorOnBadResponseHeaders(t *testing.T) {
  2677. ct := newClientTester(t)
  2678. ct.client = func() error {
  2679. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  2680. res, err := ct.tr.RoundTrip(req)
  2681. if err == nil {
  2682. res.Body.Close()
  2683. return errors.New("unexpected successful GET")
  2684. }
  2685. want := StreamError{1, ErrCodeProtocol, headerFieldNameError(" content-type")}
  2686. if !reflect.DeepEqual(want, err) {
  2687. t.Errorf("RoundTrip error = %#v; want %#v", err, want)
  2688. }
  2689. return nil
  2690. }
  2691. ct.server = func() error {
  2692. ct.greet()
  2693. hf, err := ct.firstHeaders()
  2694. if err != nil {
  2695. return err
  2696. }
  2697. var buf bytes.Buffer
  2698. enc := hpack.NewEncoder(&buf)
  2699. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2700. enc.WriteField(hpack.HeaderField{Name: " content-type", Value: "bogus"}) // bogus spaces
  2701. ct.fr.WriteHeaders(HeadersFrameParam{
  2702. StreamID: hf.StreamID,
  2703. EndHeaders: true,
  2704. EndStream: false,
  2705. BlockFragment: buf.Bytes(),
  2706. })
  2707. for {
  2708. fr, err := ct.readFrame()
  2709. if err != nil {
  2710. return fmt.Errorf("error waiting for RST_STREAM from client: %v", err)
  2711. }
  2712. if _, ok := fr.(*SettingsFrame); ok {
  2713. continue
  2714. }
  2715. if rst, ok := fr.(*RSTStreamFrame); !ok || rst.StreamID != 1 || rst.ErrCode != ErrCodeProtocol {
  2716. t.Errorf("Frame = %v; want RST_STREAM for stream 1 with ErrCodeProtocol", summarizeFrame(fr))
  2717. }
  2718. break
  2719. }
  2720. return nil
  2721. }
  2722. ct.run()
  2723. }
  2724. // byteAndEOFReader returns is in an io.Reader which reads one byte
  2725. // (the underlying byte) and io.EOF at once in its Read call.
  2726. type byteAndEOFReader byte
  2727. func (b byteAndEOFReader) Read(p []byte) (n int, err error) {
  2728. if len(p) == 0 {
  2729. panic("unexpected useless call")
  2730. }
  2731. p[0] = byte(b)
  2732. return 1, io.EOF
  2733. }
  2734. // Issue 16788: the Transport had a regression where it started
  2735. // sending a spurious DATA frame with a duplicate END_STREAM bit after
  2736. // the request body writer goroutine had already read an EOF from the
  2737. // Request.Body and included the END_STREAM on a data-carrying DATA
  2738. // frame.
  2739. //
  2740. // Notably, to trigger this, the requests need to use a Request.Body
  2741. // which returns (non-0, io.EOF) and also needs to set the ContentLength
  2742. // explicitly.
  2743. func TestTransportBodyDoubleEndStream(t *testing.T) {
  2744. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  2745. // Nothing.
  2746. }, optOnlyServer)
  2747. defer st.Close()
  2748. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  2749. defer tr.CloseIdleConnections()
  2750. for i := 0; i < 2; i++ {
  2751. req, _ := http.NewRequest("POST", st.ts.URL, byteAndEOFReader('a'))
  2752. req.ContentLength = 1
  2753. res, err := tr.RoundTrip(req)
  2754. if err != nil {
  2755. t.Fatalf("failure on req %d: %v", i+1, err)
  2756. }
  2757. defer res.Body.Close()
  2758. }
  2759. }
  2760. // golang.org/issue/16847, golang.org/issue/19103
  2761. func TestTransportRequestPathPseudo(t *testing.T) {
  2762. type result struct {
  2763. path string
  2764. err string
  2765. }
  2766. tests := []struct {
  2767. req *http.Request
  2768. want result
  2769. }{
  2770. 0: {
  2771. req: &http.Request{
  2772. Method: "GET",
  2773. URL: &url.URL{
  2774. Host: "foo.com",
  2775. Path: "/foo",
  2776. },
  2777. },
  2778. want: result{path: "/foo"},
  2779. },
  2780. // In Go 1.7, we accepted paths of "//foo".
  2781. // In Go 1.8, we rejected it (issue 16847).
  2782. // In Go 1.9, we accepted it again (issue 19103).
  2783. 1: {
  2784. req: &http.Request{
  2785. Method: "GET",
  2786. URL: &url.URL{
  2787. Host: "foo.com",
  2788. Path: "//foo",
  2789. },
  2790. },
  2791. want: result{path: "//foo"},
  2792. },
  2793. // Opaque with //$Matching_Hostname/path
  2794. 2: {
  2795. req: &http.Request{
  2796. Method: "GET",
  2797. URL: &url.URL{
  2798. Scheme: "https",
  2799. Opaque: "//foo.com/path",
  2800. Host: "foo.com",
  2801. Path: "/ignored",
  2802. },
  2803. },
  2804. want: result{path: "/path"},
  2805. },
  2806. // Opaque with some other Request.Host instead:
  2807. 3: {
  2808. req: &http.Request{
  2809. Method: "GET",
  2810. Host: "bar.com",
  2811. URL: &url.URL{
  2812. Scheme: "https",
  2813. Opaque: "//bar.com/path",
  2814. Host: "foo.com",
  2815. Path: "/ignored",
  2816. },
  2817. },
  2818. want: result{path: "/path"},
  2819. },
  2820. // Opaque without the leading "//":
  2821. 4: {
  2822. req: &http.Request{
  2823. Method: "GET",
  2824. URL: &url.URL{
  2825. Opaque: "/path",
  2826. Host: "foo.com",
  2827. Path: "/ignored",
  2828. },
  2829. },
  2830. want: result{path: "/path"},
  2831. },
  2832. // Opaque we can't handle:
  2833. 5: {
  2834. req: &http.Request{
  2835. Method: "GET",
  2836. URL: &url.URL{
  2837. Scheme: "https",
  2838. Opaque: "//unknown_host/path",
  2839. Host: "foo.com",
  2840. Path: "/ignored",
  2841. },
  2842. },
  2843. want: result{err: `invalid request :path "https://unknown_host/path" from URL.Opaque = "//unknown_host/path"`},
  2844. },
  2845. // A CONNECT request:
  2846. 6: {
  2847. req: &http.Request{
  2848. Method: "CONNECT",
  2849. URL: &url.URL{
  2850. Host: "foo.com",
  2851. },
  2852. },
  2853. want: result{},
  2854. },
  2855. }
  2856. for i, tt := range tests {
  2857. cc := &ClientConn{peerMaxHeaderListSize: 0xffffffffffffffff}
  2858. cc.henc = hpack.NewEncoder(&cc.hbuf)
  2859. cc.mu.Lock()
  2860. hdrs, err := cc.encodeHeaders(tt.req, false, "", -1)
  2861. cc.mu.Unlock()
  2862. var got result
  2863. hpackDec := hpack.NewDecoder(initialHeaderTableSize, func(f hpack.HeaderField) {
  2864. if f.Name == ":path" {
  2865. got.path = f.Value
  2866. }
  2867. })
  2868. if err != nil {
  2869. got.err = err.Error()
  2870. } else if len(hdrs) > 0 {
  2871. if _, err := hpackDec.Write(hdrs); err != nil {
  2872. t.Errorf("%d. bogus hpack: %v", i, err)
  2873. continue
  2874. }
  2875. }
  2876. if got != tt.want {
  2877. t.Errorf("%d. got %+v; want %+v", i, got, tt.want)
  2878. }
  2879. }
  2880. }
  2881. // golang.org/issue/17071 -- don't sniff the first byte of the request body
  2882. // before we've determined that the ClientConn is usable.
  2883. func TestRoundTripDoesntConsumeRequestBodyEarly(t *testing.T) {
  2884. const body = "foo"
  2885. req, _ := http.NewRequest("POST", "http://foo.com/", ioutil.NopCloser(strings.NewReader(body)))
  2886. cc := &ClientConn{
  2887. closed: true,
  2888. }
  2889. _, err := cc.RoundTrip(req)
  2890. if err != errClientConnUnusable {
  2891. t.Fatalf("RoundTrip = %v; want errClientConnUnusable", err)
  2892. }
  2893. slurp, err := ioutil.ReadAll(req.Body)
  2894. if err != nil {
  2895. t.Errorf("ReadAll = %v", err)
  2896. }
  2897. if string(slurp) != body {
  2898. t.Errorf("Body = %q; want %q", slurp, body)
  2899. }
  2900. }
  2901. func TestClientConnPing(t *testing.T) {
  2902. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {}, optOnlyServer)
  2903. defer st.Close()
  2904. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  2905. defer tr.CloseIdleConnections()
  2906. cc, err := tr.dialClientConn(st.ts.Listener.Addr().String(), false)
  2907. if err != nil {
  2908. t.Fatal(err)
  2909. }
  2910. if err = cc.Ping(context.Background()); err != nil {
  2911. t.Fatal(err)
  2912. }
  2913. }
  2914. // Issue 16974: if the server sent a DATA frame after the user
  2915. // canceled the Transport's Request, the Transport previously wrote to a
  2916. // closed pipe, got an error, and ended up closing the whole TCP
  2917. // connection.
  2918. func TestTransportCancelDataResponseRace(t *testing.T) {
  2919. cancel := make(chan struct{})
  2920. clientGotError := make(chan bool, 1)
  2921. const msg = "Hello."
  2922. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  2923. if strings.Contains(r.URL.Path, "/hello") {
  2924. time.Sleep(50 * time.Millisecond)
  2925. io.WriteString(w, msg)
  2926. return
  2927. }
  2928. for i := 0; i < 50; i++ {
  2929. io.WriteString(w, "Some data.")
  2930. w.(http.Flusher).Flush()
  2931. if i == 2 {
  2932. close(cancel)
  2933. <-clientGotError
  2934. }
  2935. time.Sleep(10 * time.Millisecond)
  2936. }
  2937. }, optOnlyServer)
  2938. defer st.Close()
  2939. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  2940. defer tr.CloseIdleConnections()
  2941. c := &http.Client{Transport: tr}
  2942. req, _ := http.NewRequest("GET", st.ts.URL, nil)
  2943. req.Cancel = cancel
  2944. res, err := c.Do(req)
  2945. if err != nil {
  2946. t.Fatal(err)
  2947. }
  2948. if _, err = io.Copy(ioutil.Discard, res.Body); err == nil {
  2949. t.Fatal("unexpected success")
  2950. }
  2951. clientGotError <- true
  2952. res, err = c.Get(st.ts.URL + "/hello")
  2953. if err != nil {
  2954. t.Fatal(err)
  2955. }
  2956. slurp, err := ioutil.ReadAll(res.Body)
  2957. if err != nil {
  2958. t.Fatal(err)
  2959. }
  2960. if string(slurp) != msg {
  2961. t.Errorf("Got = %q; want %q", slurp, msg)
  2962. }
  2963. }
  2964. // Issue 21316: It should be safe to reuse an http.Request after the
  2965. // request has completed.
  2966. func TestTransportNoRaceOnRequestObjectAfterRequestComplete(t *testing.T) {
  2967. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  2968. w.WriteHeader(200)
  2969. io.WriteString(w, "body")
  2970. }, optOnlyServer)
  2971. defer st.Close()
  2972. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  2973. defer tr.CloseIdleConnections()
  2974. req, _ := http.NewRequest("GET", st.ts.URL, nil)
  2975. resp, err := tr.RoundTrip(req)
  2976. if err != nil {
  2977. t.Fatal(err)
  2978. }
  2979. if _, err = io.Copy(ioutil.Discard, resp.Body); err != nil {
  2980. t.Fatalf("error reading response body: %v", err)
  2981. }
  2982. if err := resp.Body.Close(); err != nil {
  2983. t.Fatalf("error closing response body: %v", err)
  2984. }
  2985. // This access of req.Header should not race with code in the transport.
  2986. req.Header = http.Header{}
  2987. }
  2988. func TestTransportRetryAfterGOAWAY(t *testing.T) {
  2989. var dialer struct {
  2990. sync.Mutex
  2991. count int
  2992. }
  2993. ct1 := make(chan *clientTester)
  2994. ct2 := make(chan *clientTester)
  2995. ln := newLocalListener(t)
  2996. defer ln.Close()
  2997. tr := &Transport{
  2998. TLSClientConfig: tlsConfigInsecure,
  2999. }
  3000. tr.DialTLS = func(network, addr string, cfg *tls.Config) (net.Conn, error) {
  3001. dialer.Lock()
  3002. defer dialer.Unlock()
  3003. dialer.count++
  3004. if dialer.count == 3 {
  3005. return nil, errors.New("unexpected number of dials")
  3006. }
  3007. cc, err := net.Dial("tcp", ln.Addr().String())
  3008. if err != nil {
  3009. return nil, fmt.Errorf("dial error: %v", err)
  3010. }
  3011. sc, err := ln.Accept()
  3012. if err != nil {
  3013. return nil, fmt.Errorf("accept error: %v", err)
  3014. }
  3015. ct := &clientTester{
  3016. t: t,
  3017. tr: tr,
  3018. cc: cc,
  3019. sc: sc,
  3020. fr: NewFramer(sc, sc),
  3021. }
  3022. switch dialer.count {
  3023. case 1:
  3024. ct1 <- ct
  3025. case 2:
  3026. ct2 <- ct
  3027. }
  3028. return cc, nil
  3029. }
  3030. errs := make(chan error, 3)
  3031. done := make(chan struct{})
  3032. defer close(done)
  3033. // Client.
  3034. go func() {
  3035. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  3036. res, err := tr.RoundTrip(req)
  3037. if res != nil {
  3038. res.Body.Close()
  3039. if got := res.Header.Get("Foo"); got != "bar" {
  3040. err = fmt.Errorf("foo header = %q; want bar", got)
  3041. }
  3042. }
  3043. if err != nil {
  3044. err = fmt.Errorf("RoundTrip: %v", err)
  3045. }
  3046. errs <- err
  3047. }()
  3048. connToClose := make(chan io.Closer, 2)
  3049. // Server for the first request.
  3050. go func() {
  3051. var ct *clientTester
  3052. select {
  3053. case ct = <-ct1:
  3054. case <-done:
  3055. return
  3056. }
  3057. connToClose <- ct.cc
  3058. ct.greet()
  3059. hf, err := ct.firstHeaders()
  3060. if err != nil {
  3061. errs <- fmt.Errorf("server1 failed reading HEADERS: %v", err)
  3062. return
  3063. }
  3064. t.Logf("server1 got %v", hf)
  3065. if err := ct.fr.WriteGoAway(0 /*max id*/, ErrCodeNo, nil); err != nil {
  3066. errs <- fmt.Errorf("server1 failed writing GOAWAY: %v", err)
  3067. return
  3068. }
  3069. errs <- nil
  3070. }()
  3071. // Server for the second request.
  3072. go func() {
  3073. var ct *clientTester
  3074. select {
  3075. case ct = <-ct2:
  3076. case <-done:
  3077. return
  3078. }
  3079. connToClose <- ct.cc
  3080. ct.greet()
  3081. hf, err := ct.firstHeaders()
  3082. if err != nil {
  3083. errs <- fmt.Errorf("server2 failed reading HEADERS: %v", err)
  3084. return
  3085. }
  3086. t.Logf("server2 got %v", hf)
  3087. var buf bytes.Buffer
  3088. enc := hpack.NewEncoder(&buf)
  3089. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  3090. enc.WriteField(hpack.HeaderField{Name: "foo", Value: "bar"})
  3091. err = ct.fr.WriteHeaders(HeadersFrameParam{
  3092. StreamID: hf.StreamID,
  3093. EndHeaders: true,
  3094. EndStream: false,
  3095. BlockFragment: buf.Bytes(),
  3096. })
  3097. if err != nil {
  3098. errs <- fmt.Errorf("server2 failed writing response HEADERS: %v", err)
  3099. } else {
  3100. errs <- nil
  3101. }
  3102. }()
  3103. for k := 0; k < 3; k++ {
  3104. select {
  3105. case err := <-errs:
  3106. if err != nil {
  3107. t.Error(err)
  3108. }
  3109. case <-time.After(1 * time.Second):
  3110. t.Errorf("timed out")
  3111. }
  3112. }
  3113. for {
  3114. select {
  3115. case c := <-connToClose:
  3116. c.Close()
  3117. default:
  3118. return
  3119. }
  3120. }
  3121. }
  3122. func TestTransportRetryAfterRefusedStream(t *testing.T) {
  3123. clientDone := make(chan struct{})
  3124. ct := newClientTester(t)
  3125. ct.client = func() error {
  3126. defer ct.cc.(*net.TCPConn).CloseWrite()
  3127. defer close(clientDone)
  3128. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  3129. resp, err := ct.tr.RoundTrip(req)
  3130. if err != nil {
  3131. return fmt.Errorf("RoundTrip: %v", err)
  3132. }
  3133. resp.Body.Close()
  3134. if resp.StatusCode != 204 {
  3135. return fmt.Errorf("Status = %v; want 204", resp.StatusCode)
  3136. }
  3137. return nil
  3138. }
  3139. ct.server = func() error {
  3140. ct.greet()
  3141. var buf bytes.Buffer
  3142. enc := hpack.NewEncoder(&buf)
  3143. nreq := 0
  3144. for {
  3145. f, err := ct.fr.ReadFrame()
  3146. if err != nil {
  3147. select {
  3148. case <-clientDone:
  3149. // If the client's done, it
  3150. // will have reported any
  3151. // errors on its side.
  3152. return nil
  3153. default:
  3154. return err
  3155. }
  3156. }
  3157. switch f := f.(type) {
  3158. case *WindowUpdateFrame, *SettingsFrame:
  3159. case *HeadersFrame:
  3160. if !f.HeadersEnded() {
  3161. return fmt.Errorf("headers should have END_HEADERS be ended: %v", f)
  3162. }
  3163. nreq++
  3164. if nreq == 1 {
  3165. ct.fr.WriteRSTStream(f.StreamID, ErrCodeRefusedStream)
  3166. } else {
  3167. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "204"})
  3168. ct.fr.WriteHeaders(HeadersFrameParam{
  3169. StreamID: f.StreamID,
  3170. EndHeaders: true,
  3171. EndStream: true,
  3172. BlockFragment: buf.Bytes(),
  3173. })
  3174. }
  3175. default:
  3176. return fmt.Errorf("Unexpected client frame %v", f)
  3177. }
  3178. }
  3179. }
  3180. ct.run()
  3181. }
  3182. func TestTransportRetryHasLimit(t *testing.T) {
  3183. // Skip in short mode because the total expected delay is 1s+2s+4s+8s+16s=29s.
  3184. if testing.Short() {
  3185. t.Skip("skipping long test in short mode")
  3186. }
  3187. clientDone := make(chan struct{})
  3188. ct := newClientTester(t)
  3189. ct.client = func() error {
  3190. defer ct.cc.(*net.TCPConn).CloseWrite()
  3191. defer close(clientDone)
  3192. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  3193. resp, err := ct.tr.RoundTrip(req)
  3194. if err == nil {
  3195. return fmt.Errorf("RoundTrip expected error, got response: %+v", resp)
  3196. }
  3197. t.Logf("expected error, got: %v", err)
  3198. return nil
  3199. }
  3200. ct.server = func() error {
  3201. ct.greet()
  3202. for {
  3203. f, err := ct.fr.ReadFrame()
  3204. if err != nil {
  3205. select {
  3206. case <-clientDone:
  3207. // If the client's done, it
  3208. // will have reported any
  3209. // errors on its side.
  3210. return nil
  3211. default:
  3212. return err
  3213. }
  3214. }
  3215. switch f := f.(type) {
  3216. case *WindowUpdateFrame, *SettingsFrame:
  3217. case *HeadersFrame:
  3218. if !f.HeadersEnded() {
  3219. return fmt.Errorf("headers should have END_HEADERS be ended: %v", f)
  3220. }
  3221. ct.fr.WriteRSTStream(f.StreamID, ErrCodeRefusedStream)
  3222. default:
  3223. return fmt.Errorf("Unexpected client frame %v", f)
  3224. }
  3225. }
  3226. }
  3227. ct.run()
  3228. }
  3229. func TestTransportResponseDataBeforeHeaders(t *testing.T) {
  3230. // This test use not valid response format.
  3231. // Discarding logger output to not spam tests output.
  3232. log.SetOutput(ioutil.Discard)
  3233. defer log.SetOutput(os.Stderr)
  3234. ct := newClientTester(t)
  3235. ct.client = func() error {
  3236. defer ct.cc.(*net.TCPConn).CloseWrite()
  3237. req := httptest.NewRequest("GET", "https://dummy.tld/", nil)
  3238. // First request is normal to ensure the check is per stream and not per connection.
  3239. _, err := ct.tr.RoundTrip(req)
  3240. if err != nil {
  3241. return fmt.Errorf("RoundTrip expected no error, got: %v", err)
  3242. }
  3243. // Second request returns a DATA frame with no HEADERS.
  3244. resp, err := ct.tr.RoundTrip(req)
  3245. if err == nil {
  3246. return fmt.Errorf("RoundTrip expected error, got response: %+v", resp)
  3247. }
  3248. if err, ok := err.(StreamError); !ok || err.Code != ErrCodeProtocol {
  3249. return fmt.Errorf("expected stream PROTOCOL_ERROR, got: %v", err)
  3250. }
  3251. return nil
  3252. }
  3253. ct.server = func() error {
  3254. ct.greet()
  3255. for {
  3256. f, err := ct.fr.ReadFrame()
  3257. if err == io.EOF {
  3258. return nil
  3259. } else if err != nil {
  3260. return err
  3261. }
  3262. switch f := f.(type) {
  3263. case *WindowUpdateFrame, *SettingsFrame:
  3264. case *HeadersFrame:
  3265. switch f.StreamID {
  3266. case 1:
  3267. // Send a valid response to first request.
  3268. var buf bytes.Buffer
  3269. enc := hpack.NewEncoder(&buf)
  3270. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  3271. ct.fr.WriteHeaders(HeadersFrameParam{
  3272. StreamID: f.StreamID,
  3273. EndHeaders: true,
  3274. EndStream: true,
  3275. BlockFragment: buf.Bytes(),
  3276. })
  3277. case 3:
  3278. ct.fr.WriteData(f.StreamID, true, []byte("payload"))
  3279. }
  3280. default:
  3281. return fmt.Errorf("Unexpected client frame %v", f)
  3282. }
  3283. }
  3284. }
  3285. ct.run()
  3286. }
  3287. // tests Transport.StrictMaxConcurrentStreams
  3288. func TestTransportRequestsStallAtServerLimit(t *testing.T) {
  3289. const maxConcurrent = 2
  3290. greet := make(chan struct{}) // server sends initial SETTINGS frame
  3291. gotRequest := make(chan struct{}) // server received a request
  3292. clientDone := make(chan struct{})
  3293. // Collect errors from goroutines.
  3294. var wg sync.WaitGroup
  3295. errs := make(chan error, 100)
  3296. defer func() {
  3297. wg.Wait()
  3298. close(errs)
  3299. for err := range errs {
  3300. t.Error(err)
  3301. }
  3302. }()
  3303. // We will send maxConcurrent+2 requests. This checker goroutine waits for the
  3304. // following stages:
  3305. // 1. The first maxConcurrent requests are received by the server.
  3306. // 2. The client will cancel the next request
  3307. // 3. The server is unblocked so it can service the first maxConcurrent requests
  3308. // 4. The client will send the final request
  3309. wg.Add(1)
  3310. unblockClient := make(chan struct{})
  3311. clientRequestCancelled := make(chan struct{})
  3312. unblockServer := make(chan struct{})
  3313. go func() {
  3314. defer wg.Done()
  3315. // Stage 1.
  3316. for k := 0; k < maxConcurrent; k++ {
  3317. <-gotRequest
  3318. }
  3319. // Stage 2.
  3320. close(unblockClient)
  3321. <-clientRequestCancelled
  3322. // Stage 3: give some time for the final RoundTrip call to be scheduled and
  3323. // verify that the final request is not sent.
  3324. time.Sleep(50 * time.Millisecond)
  3325. select {
  3326. case <-gotRequest:
  3327. errs <- errors.New("last request did not stall")
  3328. close(unblockServer)
  3329. return
  3330. default:
  3331. }
  3332. close(unblockServer)
  3333. // Stage 4.
  3334. <-gotRequest
  3335. }()
  3336. ct := newClientTester(t)
  3337. ct.tr.StrictMaxConcurrentStreams = true
  3338. ct.client = func() error {
  3339. var wg sync.WaitGroup
  3340. defer func() {
  3341. wg.Wait()
  3342. close(clientDone)
  3343. ct.cc.(*net.TCPConn).CloseWrite()
  3344. }()
  3345. for k := 0; k < maxConcurrent+2; k++ {
  3346. wg.Add(1)
  3347. go func(k int) {
  3348. defer wg.Done()
  3349. // Don't send the second request until after receiving SETTINGS from the server
  3350. // to avoid a race where we use the default SettingMaxConcurrentStreams, which
  3351. // is much larger than maxConcurrent. We have to send the first request before
  3352. // waiting because the first request triggers the dial and greet.
  3353. if k > 0 {
  3354. <-greet
  3355. }
  3356. // Block until maxConcurrent requests are sent before sending any more.
  3357. if k >= maxConcurrent {
  3358. <-unblockClient
  3359. }
  3360. req, _ := http.NewRequest("GET", fmt.Sprintf("https://dummy.tld/%d", k), nil)
  3361. if k == maxConcurrent {
  3362. // This request will be canceled.
  3363. cancel := make(chan struct{})
  3364. req.Cancel = cancel
  3365. close(cancel)
  3366. _, err := ct.tr.RoundTrip(req)
  3367. close(clientRequestCancelled)
  3368. if err == nil {
  3369. errs <- fmt.Errorf("RoundTrip(%d) should have failed due to cancel", k)
  3370. return
  3371. }
  3372. } else {
  3373. resp, err := ct.tr.RoundTrip(req)
  3374. if err != nil {
  3375. errs <- fmt.Errorf("RoundTrip(%d): %v", k, err)
  3376. return
  3377. }
  3378. ioutil.ReadAll(resp.Body)
  3379. resp.Body.Close()
  3380. if resp.StatusCode != 204 {
  3381. errs <- fmt.Errorf("Status = %v; want 204", resp.StatusCode)
  3382. return
  3383. }
  3384. }
  3385. }(k)
  3386. }
  3387. return nil
  3388. }
  3389. ct.server = func() error {
  3390. var wg sync.WaitGroup
  3391. defer wg.Wait()
  3392. ct.greet(Setting{SettingMaxConcurrentStreams, maxConcurrent})
  3393. // Server write loop.
  3394. var buf bytes.Buffer
  3395. enc := hpack.NewEncoder(&buf)
  3396. writeResp := make(chan uint32, maxConcurrent+1)
  3397. wg.Add(1)
  3398. go func() {
  3399. defer wg.Done()
  3400. <-unblockServer
  3401. for id := range writeResp {
  3402. buf.Reset()
  3403. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "204"})
  3404. ct.fr.WriteHeaders(HeadersFrameParam{
  3405. StreamID: id,
  3406. EndHeaders: true,
  3407. EndStream: true,
  3408. BlockFragment: buf.Bytes(),
  3409. })
  3410. }
  3411. }()
  3412. // Server read loop.
  3413. var nreq int
  3414. for {
  3415. f, err := ct.fr.ReadFrame()
  3416. if err != nil {
  3417. select {
  3418. case <-clientDone:
  3419. // If the client's done, it will have reported any errors on its side.
  3420. return nil
  3421. default:
  3422. return err
  3423. }
  3424. }
  3425. switch f := f.(type) {
  3426. case *WindowUpdateFrame:
  3427. case *SettingsFrame:
  3428. // Wait for the client SETTINGS ack until ending the greet.
  3429. close(greet)
  3430. case *HeadersFrame:
  3431. if !f.HeadersEnded() {
  3432. return fmt.Errorf("headers should have END_HEADERS be ended: %v", f)
  3433. }
  3434. gotRequest <- struct{}{}
  3435. nreq++
  3436. writeResp <- f.StreamID
  3437. if nreq == maxConcurrent+1 {
  3438. close(writeResp)
  3439. }
  3440. default:
  3441. return fmt.Errorf("Unexpected client frame %v", f)
  3442. }
  3443. }
  3444. }
  3445. ct.run()
  3446. }
  3447. func TestAuthorityAddr(t *testing.T) {
  3448. tests := []struct {
  3449. scheme, authority string
  3450. want string
  3451. }{
  3452. {"http", "foo.com", "foo.com:80"},
  3453. {"https", "foo.com", "foo.com:443"},
  3454. {"https", "foo.com:1234", "foo.com:1234"},
  3455. {"https", "1.2.3.4:1234", "1.2.3.4:1234"},
  3456. {"https", "1.2.3.4", "1.2.3.4:443"},
  3457. {"https", "[::1]:1234", "[::1]:1234"},
  3458. {"https", "[::1]", "[::1]:443"},
  3459. }
  3460. for _, tt := range tests {
  3461. got := authorityAddr(tt.scheme, tt.authority)
  3462. if got != tt.want {
  3463. t.Errorf("authorityAddr(%q, %q) = %q; want %q", tt.scheme, tt.authority, got, tt.want)
  3464. }
  3465. }
  3466. }
  3467. // Issue 20448: stop allocating for DATA frames' payload after
  3468. // Response.Body.Close is called.
  3469. func TestTransportAllocationsAfterResponseBodyClose(t *testing.T) {
  3470. megabyteZero := make([]byte, 1<<20)
  3471. writeErr := make(chan error, 1)
  3472. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  3473. w.(http.Flusher).Flush()
  3474. var sum int64
  3475. for i := 0; i < 100; i++ {
  3476. n, err := w.Write(megabyteZero)
  3477. sum += int64(n)
  3478. if err != nil {
  3479. writeErr <- err
  3480. return
  3481. }
  3482. }
  3483. t.Logf("wrote all %d bytes", sum)
  3484. writeErr <- nil
  3485. }, optOnlyServer)
  3486. defer st.Close()
  3487. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  3488. defer tr.CloseIdleConnections()
  3489. c := &http.Client{Transport: tr}
  3490. res, err := c.Get(st.ts.URL)
  3491. if err != nil {
  3492. t.Fatal(err)
  3493. }
  3494. var buf [1]byte
  3495. if _, err := res.Body.Read(buf[:]); err != nil {
  3496. t.Error(err)
  3497. }
  3498. if err := res.Body.Close(); err != nil {
  3499. t.Error(err)
  3500. }
  3501. trb, ok := res.Body.(transportResponseBody)
  3502. if !ok {
  3503. t.Fatalf("res.Body = %T; want transportResponseBody", res.Body)
  3504. }
  3505. if trb.cs.bufPipe.b != nil {
  3506. t.Errorf("response body pipe is still open")
  3507. }
  3508. gotErr := <-writeErr
  3509. if gotErr == nil {
  3510. t.Errorf("Handler unexpectedly managed to write its entire response without getting an error")
  3511. } else if gotErr != errStreamClosed {
  3512. t.Errorf("Handler Write err = %v; want errStreamClosed", gotErr)
  3513. }
  3514. }
  3515. // Issue 18891: make sure Request.Body == NoBody means no DATA frame
  3516. // is ever sent, even if empty.
  3517. func TestTransportNoBodyMeansNoDATA(t *testing.T) {
  3518. ct := newClientTester(t)
  3519. unblockClient := make(chan bool)
  3520. ct.client = func() error {
  3521. req, _ := http.NewRequest("GET", "https://dummy.tld/", http.NoBody)
  3522. ct.tr.RoundTrip(req)
  3523. <-unblockClient
  3524. return nil
  3525. }
  3526. ct.server = func() error {
  3527. defer close(unblockClient)
  3528. defer ct.cc.(*net.TCPConn).Close()
  3529. ct.greet()
  3530. for {
  3531. f, err := ct.fr.ReadFrame()
  3532. if err != nil {
  3533. return fmt.Errorf("ReadFrame while waiting for Headers: %v", err)
  3534. }
  3535. switch f := f.(type) {
  3536. default:
  3537. return fmt.Errorf("Got %T; want HeadersFrame", f)
  3538. case *WindowUpdateFrame, *SettingsFrame:
  3539. continue
  3540. case *HeadersFrame:
  3541. if !f.StreamEnded() {
  3542. return fmt.Errorf("got headers frame without END_STREAM")
  3543. }
  3544. return nil
  3545. }
  3546. }
  3547. }
  3548. ct.run()
  3549. }
  3550. func benchSimpleRoundTrip(b *testing.B, nHeaders int) {
  3551. defer disableGoroutineTracking()()
  3552. b.ReportAllocs()
  3553. st := newServerTester(b,
  3554. func(w http.ResponseWriter, r *http.Request) {
  3555. },
  3556. optOnlyServer,
  3557. optQuiet,
  3558. )
  3559. defer st.Close()
  3560. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  3561. defer tr.CloseIdleConnections()
  3562. req, err := http.NewRequest("GET", st.ts.URL, nil)
  3563. if err != nil {
  3564. b.Fatal(err)
  3565. }
  3566. for i := 0; i < nHeaders; i++ {
  3567. name := fmt.Sprint("A-", i)
  3568. req.Header.Set(name, "*")
  3569. }
  3570. b.ResetTimer()
  3571. for i := 0; i < b.N; i++ {
  3572. res, err := tr.RoundTrip(req)
  3573. if err != nil {
  3574. if res != nil {
  3575. res.Body.Close()
  3576. }
  3577. b.Fatalf("RoundTrip err = %v; want nil", err)
  3578. }
  3579. res.Body.Close()
  3580. if res.StatusCode != http.StatusOK {
  3581. b.Fatalf("Response code = %v; want %v", res.StatusCode, http.StatusOK)
  3582. }
  3583. }
  3584. }
  3585. type infiniteReader struct{}
  3586. func (r infiniteReader) Read(b []byte) (int, error) {
  3587. return len(b), nil
  3588. }
  3589. // Issue 20521: it is not an error to receive a response and end stream
  3590. // from the server without the body being consumed.
  3591. func TestTransportResponseAndResetWithoutConsumingBodyRace(t *testing.T) {
  3592. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  3593. w.WriteHeader(http.StatusOK)
  3594. }, optOnlyServer)
  3595. defer st.Close()
  3596. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  3597. defer tr.CloseIdleConnections()
  3598. // The request body needs to be big enough to trigger flow control.
  3599. req, _ := http.NewRequest("PUT", st.ts.URL, infiniteReader{})
  3600. res, err := tr.RoundTrip(req)
  3601. if err != nil {
  3602. t.Fatal(err)
  3603. }
  3604. if res.StatusCode != http.StatusOK {
  3605. t.Fatalf("Response code = %v; want %v", res.StatusCode, http.StatusOK)
  3606. }
  3607. }
  3608. // Verify transport doesn't crash when receiving bogus response lacking a :status header.
  3609. // Issue 22880.
  3610. func TestTransportHandlesInvalidStatuslessResponse(t *testing.T) {
  3611. ct := newClientTester(t)
  3612. ct.client = func() error {
  3613. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  3614. _, err := ct.tr.RoundTrip(req)
  3615. const substr = "malformed response from server: missing status pseudo header"
  3616. if !strings.Contains(fmt.Sprint(err), substr) {
  3617. return fmt.Errorf("RoundTrip error = %v; want substring %q", err, substr)
  3618. }
  3619. return nil
  3620. }
  3621. ct.server = func() error {
  3622. ct.greet()
  3623. var buf bytes.Buffer
  3624. enc := hpack.NewEncoder(&buf)
  3625. for {
  3626. f, err := ct.fr.ReadFrame()
  3627. if err != nil {
  3628. return err
  3629. }
  3630. switch f := f.(type) {
  3631. case *HeadersFrame:
  3632. enc.WriteField(hpack.HeaderField{Name: "content-type", Value: "text/html"}) // no :status header
  3633. ct.fr.WriteHeaders(HeadersFrameParam{
  3634. StreamID: f.StreamID,
  3635. EndHeaders: true,
  3636. EndStream: false, // we'll send some DATA to try to crash the transport
  3637. BlockFragment: buf.Bytes(),
  3638. })
  3639. ct.fr.WriteData(f.StreamID, true, []byte("payload"))
  3640. return nil
  3641. }
  3642. }
  3643. }
  3644. ct.run()
  3645. }
  3646. func BenchmarkClientRequestHeaders(b *testing.B) {
  3647. b.Run(" 0 Headers", func(b *testing.B) { benchSimpleRoundTrip(b, 0) })
  3648. b.Run(" 10 Headers", func(b *testing.B) { benchSimpleRoundTrip(b, 10) })
  3649. b.Run(" 100 Headers", func(b *testing.B) { benchSimpleRoundTrip(b, 100) })
  3650. b.Run("1000 Headers", func(b *testing.B) { benchSimpleRoundTrip(b, 1000) })
  3651. }
  3652. func activeStreams(cc *ClientConn) int {
  3653. cc.mu.Lock()
  3654. defer cc.mu.Unlock()
  3655. return len(cc.streams)
  3656. }
  3657. type closeMode int
  3658. const (
  3659. closeAtHeaders closeMode = iota
  3660. closeAtBody
  3661. shutdown
  3662. shutdownCancel
  3663. )
  3664. // See golang.org/issue/17292
  3665. func testClientConnClose(t *testing.T, closeMode closeMode) {
  3666. clientDone := make(chan struct{})
  3667. defer close(clientDone)
  3668. handlerDone := make(chan struct{})
  3669. closeDone := make(chan struct{})
  3670. beforeHeader := func() {}
  3671. bodyWrite := func(w http.ResponseWriter) {}
  3672. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  3673. defer close(handlerDone)
  3674. beforeHeader()
  3675. w.WriteHeader(http.StatusOK)
  3676. w.(http.Flusher).Flush()
  3677. bodyWrite(w)
  3678. select {
  3679. case <-w.(http.CloseNotifier).CloseNotify():
  3680. // client closed connection before completion
  3681. if closeMode == shutdown || closeMode == shutdownCancel {
  3682. t.Error("expected request to complete")
  3683. }
  3684. case <-clientDone:
  3685. if closeMode == closeAtHeaders || closeMode == closeAtBody {
  3686. t.Error("expected connection closed by client")
  3687. }
  3688. }
  3689. }, optOnlyServer)
  3690. defer st.Close()
  3691. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  3692. defer tr.CloseIdleConnections()
  3693. cc, err := tr.dialClientConn(st.ts.Listener.Addr().String(), false)
  3694. req, err := http.NewRequest("GET", st.ts.URL, nil)
  3695. if err != nil {
  3696. t.Fatal(err)
  3697. }
  3698. if closeMode == closeAtHeaders {
  3699. beforeHeader = func() {
  3700. if err := cc.Close(); err != nil {
  3701. t.Error(err)
  3702. }
  3703. close(closeDone)
  3704. }
  3705. }
  3706. var sendBody chan struct{}
  3707. if closeMode == closeAtBody {
  3708. sendBody = make(chan struct{})
  3709. bodyWrite = func(w http.ResponseWriter) {
  3710. <-sendBody
  3711. b := make([]byte, 32)
  3712. w.Write(b)
  3713. w.(http.Flusher).Flush()
  3714. if err := cc.Close(); err != nil {
  3715. t.Errorf("unexpected ClientConn close error: %v", err)
  3716. }
  3717. close(closeDone)
  3718. w.Write(b)
  3719. w.(http.Flusher).Flush()
  3720. }
  3721. }
  3722. res, err := cc.RoundTrip(req)
  3723. if res != nil {
  3724. defer res.Body.Close()
  3725. }
  3726. if closeMode == closeAtHeaders {
  3727. got := fmt.Sprint(err)
  3728. want := "http2: client connection force closed via ClientConn.Close"
  3729. if got != want {
  3730. t.Fatalf("RoundTrip error = %v, want %v", got, want)
  3731. }
  3732. } else {
  3733. if err != nil {
  3734. t.Fatalf("RoundTrip: %v", err)
  3735. }
  3736. if got, want := activeStreams(cc), 1; got != want {
  3737. t.Errorf("got %d active streams, want %d", got, want)
  3738. }
  3739. }
  3740. switch closeMode {
  3741. case shutdownCancel:
  3742. if err = cc.Shutdown(canceledCtx); err != context.Canceled {
  3743. t.Errorf("got %v, want %v", err, context.Canceled)
  3744. }
  3745. if cc.closing == false {
  3746. t.Error("expected closing to be true")
  3747. }
  3748. if cc.CanTakeNewRequest() == true {
  3749. t.Error("CanTakeNewRequest to return false")
  3750. }
  3751. if v, want := len(cc.streams), 1; v != want {
  3752. t.Errorf("expected %d active streams, got %d", want, v)
  3753. }
  3754. clientDone <- struct{}{}
  3755. <-handlerDone
  3756. case shutdown:
  3757. wait := make(chan struct{})
  3758. shutdownEnterWaitStateHook = func() {
  3759. close(wait)
  3760. shutdownEnterWaitStateHook = func() {}
  3761. }
  3762. defer func() { shutdownEnterWaitStateHook = func() {} }()
  3763. shutdown := make(chan struct{}, 1)
  3764. go func() {
  3765. if err = cc.Shutdown(context.Background()); err != nil {
  3766. t.Error(err)
  3767. }
  3768. close(shutdown)
  3769. }()
  3770. // Let the shutdown to enter wait state
  3771. <-wait
  3772. cc.mu.Lock()
  3773. if cc.closing == false {
  3774. t.Error("expected closing to be true")
  3775. }
  3776. cc.mu.Unlock()
  3777. if cc.CanTakeNewRequest() == true {
  3778. t.Error("CanTakeNewRequest to return false")
  3779. }
  3780. if got, want := activeStreams(cc), 1; got != want {
  3781. t.Errorf("got %d active streams, want %d", got, want)
  3782. }
  3783. // Let the active request finish
  3784. clientDone <- struct{}{}
  3785. // Wait for the shutdown to end
  3786. select {
  3787. case <-shutdown:
  3788. case <-time.After(2 * time.Second):
  3789. t.Fatal("expected server connection to close")
  3790. }
  3791. case closeAtHeaders, closeAtBody:
  3792. if closeMode == closeAtBody {
  3793. go close(sendBody)
  3794. if _, err := io.Copy(ioutil.Discard, res.Body); err == nil {
  3795. t.Error("expected a Copy error, got nil")
  3796. }
  3797. }
  3798. <-closeDone
  3799. if got, want := activeStreams(cc), 0; got != want {
  3800. t.Errorf("got %d active streams, want %d", got, want)
  3801. }
  3802. // wait for server to get the connection close notice
  3803. select {
  3804. case <-handlerDone:
  3805. case <-time.After(2 * time.Second):
  3806. t.Fatal("expected server connection to close")
  3807. }
  3808. }
  3809. }
  3810. // The client closes the connection just after the server got the client's HEADERS
  3811. // frame, but before the server sends its HEADERS response back. The expected
  3812. // result is an error on RoundTrip explaining the client closed the connection.
  3813. func TestClientConnCloseAtHeaders(t *testing.T) {
  3814. testClientConnClose(t, closeAtHeaders)
  3815. }
  3816. // The client closes the connection between two server's response DATA frames.
  3817. // The expected behavior is a response body io read error on the client.
  3818. func TestClientConnCloseAtBody(t *testing.T) {
  3819. testClientConnClose(t, closeAtBody)
  3820. }
  3821. // The client sends a GOAWAY frame before the server finished processing a request.
  3822. // We expect the connection not to close until the request is completed.
  3823. func TestClientConnShutdown(t *testing.T) {
  3824. testClientConnClose(t, shutdown)
  3825. }
  3826. // The client sends a GOAWAY frame before the server finishes processing a request,
  3827. // but cancels the passed context before the request is completed. The expected
  3828. // behavior is the client closing the connection after the context is canceled.
  3829. func TestClientConnShutdownCancel(t *testing.T) {
  3830. testClientConnClose(t, shutdownCancel)
  3831. }
  3832. // Issue 25009: use Request.GetBody if present, even if it seems like
  3833. // we might not need it. Apparently something else can still read from
  3834. // the original request body. Data race? In any case, rewinding
  3835. // unconditionally on retry is a nicer model anyway and should
  3836. // simplify code in the future (after the Go 1.11 freeze)
  3837. func TestTransportUsesGetBodyWhenPresent(t *testing.T) {
  3838. calls := 0
  3839. someBody := func() io.ReadCloser {
  3840. return struct{ io.ReadCloser }{ioutil.NopCloser(bytes.NewReader(nil))}
  3841. }
  3842. req := &http.Request{
  3843. Body: someBody(),
  3844. GetBody: func() (io.ReadCloser, error) {
  3845. calls++
  3846. return someBody(), nil
  3847. },
  3848. }
  3849. afterBodyWrite := false // pretend we haven't read+written the body yet
  3850. req2, err := shouldRetryRequest(req, errClientConnUnusable, afterBodyWrite)
  3851. if err != nil {
  3852. t.Fatal(err)
  3853. }
  3854. if calls != 1 {
  3855. t.Errorf("Calls = %d; want 1", calls)
  3856. }
  3857. if req2 == req {
  3858. t.Error("req2 changed")
  3859. }
  3860. if req2 == nil {
  3861. t.Fatal("req2 is nil")
  3862. }
  3863. if req2.Body == nil {
  3864. t.Fatal("req2.Body is nil")
  3865. }
  3866. if req2.GetBody == nil {
  3867. t.Fatal("req2.GetBody is nil")
  3868. }
  3869. if req2.Body == req.Body {
  3870. t.Error("req2.Body unchanged")
  3871. }
  3872. }
  3873. // Issue 22891: verify that the "https" altproto we register with net/http
  3874. // is a certain type: a struct with one field with our *http2.Transport in it.
  3875. func TestNoDialH2RoundTripperType(t *testing.T) {
  3876. t1 := new(http.Transport)
  3877. t2 := new(Transport)
  3878. rt := noDialH2RoundTripper{t2}
  3879. if err := registerHTTPSProtocol(t1, rt); err != nil {
  3880. t.Fatal(err)
  3881. }
  3882. rv := reflect.ValueOf(rt)
  3883. if rv.Type().Kind() != reflect.Struct {
  3884. t.Fatalf("kind = %v; net/http expects struct", rv.Type().Kind())
  3885. }
  3886. if n := rv.Type().NumField(); n != 1 {
  3887. t.Fatalf("fields = %d; net/http expects 1", n)
  3888. }
  3889. v := rv.Field(0)
  3890. if _, ok := v.Interface().(*Transport); !ok {
  3891. t.Fatalf("wrong kind %T; want *Transport", v.Interface())
  3892. }
  3893. }
  3894. type errReader struct {
  3895. body []byte
  3896. err error
  3897. }
  3898. func (r *errReader) Read(p []byte) (int, error) {
  3899. if len(r.body) > 0 {
  3900. n := copy(p, r.body)
  3901. r.body = r.body[n:]
  3902. return n, nil
  3903. }
  3904. return 0, r.err
  3905. }
  3906. func testTransportBodyReadError(t *testing.T, body []byte) {
  3907. clientDone := make(chan struct{})
  3908. ct := newClientTester(t)
  3909. ct.client = func() error {
  3910. defer ct.cc.(*net.TCPConn).CloseWrite()
  3911. defer close(clientDone)
  3912. checkNoStreams := func() error {
  3913. cp, ok := ct.tr.connPool().(*clientConnPool)
  3914. if !ok {
  3915. return fmt.Errorf("conn pool is %T; want *clientConnPool", ct.tr.connPool())
  3916. }
  3917. cp.mu.Lock()
  3918. defer cp.mu.Unlock()
  3919. conns, ok := cp.conns["dummy.tld:443"]
  3920. if !ok {
  3921. return fmt.Errorf("missing connection")
  3922. }
  3923. if len(conns) != 1 {
  3924. return fmt.Errorf("conn pool size: %v; expect 1", len(conns))
  3925. }
  3926. if activeStreams(conns[0]) != 0 {
  3927. return fmt.Errorf("active streams count: %v; want 0", activeStreams(conns[0]))
  3928. }
  3929. return nil
  3930. }
  3931. bodyReadError := errors.New("body read error")
  3932. body := &errReader{body, bodyReadError}
  3933. req, err := http.NewRequest("PUT", "https://dummy.tld/", body)
  3934. if err != nil {
  3935. return err
  3936. }
  3937. _, err = ct.tr.RoundTrip(req)
  3938. if err != bodyReadError {
  3939. return fmt.Errorf("err = %v; want %v", err, bodyReadError)
  3940. }
  3941. if err = checkNoStreams(); err != nil {
  3942. return err
  3943. }
  3944. return nil
  3945. }
  3946. ct.server = func() error {
  3947. ct.greet()
  3948. var receivedBody []byte
  3949. var resetCount int
  3950. for {
  3951. f, err := ct.fr.ReadFrame()
  3952. if err != nil {
  3953. select {
  3954. case <-clientDone:
  3955. // If the client's done, it
  3956. // will have reported any
  3957. // errors on its side.
  3958. if bytes.Compare(receivedBody, body) != 0 {
  3959. return fmt.Errorf("body: %v; expected %v", receivedBody, body)
  3960. }
  3961. if resetCount != 1 {
  3962. return fmt.Errorf("stream reset count: %v; expected: 1", resetCount)
  3963. }
  3964. return nil
  3965. default:
  3966. return err
  3967. }
  3968. }
  3969. switch f := f.(type) {
  3970. case *WindowUpdateFrame, *SettingsFrame:
  3971. case *HeadersFrame:
  3972. case *DataFrame:
  3973. receivedBody = append(receivedBody, f.Data()...)
  3974. case *RSTStreamFrame:
  3975. resetCount++
  3976. default:
  3977. return fmt.Errorf("Unexpected client frame %v", f)
  3978. }
  3979. }
  3980. }
  3981. ct.run()
  3982. }
  3983. func TestTransportBodyReadError_Immediately(t *testing.T) { testTransportBodyReadError(t, nil) }
  3984. func TestTransportBodyReadError_Some(t *testing.T) { testTransportBodyReadError(t, []byte("123")) }