Source file src/net/http/h2_error_test.go

     1  // Copyright 2022 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  
     5  //go:build !nethttpomithttp2
     6  
     7  package http
     8  
     9  import (
    10  	"errors"
    11  	"fmt"
    12  	"net/http/internal/http2"
    13  	"testing"
    14  )
    15  
    16  type externalStreamErrorCode uint32
    17  
    18  type externalStreamError struct {
    19  	StreamID uint32
    20  	Code     externalStreamErrorCode
    21  	Cause    error
    22  }
    23  
    24  var _ error = externalStreamError{}
    25  
    26  func (e externalStreamError) Error() string {
    27  	return fmt.Sprintf("ID %v, code %v", e.StreamID, e.Code)
    28  }
    29  
    30  func TestStreamError(t *testing.T) {
    31  	streamErr := http2.StreamError{StreamID: 42, Code: http2.ErrCodeProtocol}
    32  	extStreamErr, ok := errors.AsType[externalStreamError](streamErr)
    33  	if !ok {
    34  		t.Fatalf("errors.AsType failed")
    35  	}
    36  	if extStreamErr.StreamID != streamErr.StreamID {
    37  		t.Errorf("got StreamID %v, expected %v", extStreamErr.StreamID, streamErr.StreamID)
    38  	}
    39  	if extStreamErr.Cause != streamErr.Cause {
    40  		t.Errorf("got Cause %v, expected %v", extStreamErr.Cause, streamErr.Cause)
    41  	}
    42  	if uint32(extStreamErr.Code) != uint32(streamErr.Code) {
    43  		t.Errorf("got Code %v, expected %v", extStreamErr.Code, streamErr.Code)
    44  	}
    45  }
    46  

View as plain text