File: internal_response_writer_test.go

package info (click to toggle)
golang-github-newrelic-go-agent 3.15.2-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 8,356 kB
  • sloc: sh: 65; makefile: 6
file content (109 lines) | stat: -rw-r--r-- 2,615 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package newrelic

import (
	"bufio"
	"io"
	"net"
	"net/http"
	"testing"
)

type rwNoExtraMethods struct {
	hijackCalled      bool
	readFromCalled    bool
	flushCalled       bool
	closeNotifyCalled bool
}

type rwTwoExtraMethods struct{ rwNoExtraMethods }
type rwAllExtraMethods struct{ rwTwoExtraMethods }

func (rw *rwAllExtraMethods) CloseNotify() <-chan bool {
	rw.closeNotifyCalled = true
	return nil
}
func (rw *rwAllExtraMethods) ReadFrom(r io.Reader) (int64, error) {
	rw.readFromCalled = true
	return 0, nil
}

func (rw *rwNoExtraMethods) Header() http.Header        { return nil }
func (rw *rwNoExtraMethods) Write([]byte) (int, error)  { return 0, nil }
func (rw *rwNoExtraMethods) WriteHeader(statusCode int) {}

func (rw *rwTwoExtraMethods) Flush() {
	rw.flushCalled = true
}
func (rw *rwTwoExtraMethods) Hijack() (net.Conn, *bufio.ReadWriter, error) {
	rw.hijackCalled = true
	return nil, nil, nil
}

func TestTransactionAllExtraMethods(t *testing.T) {
	app := testApp(nil, nil, t)
	rw := &rwAllExtraMethods{}
	txn := app.StartTransaction("hello", rw, nil)
	if v, ok := txn.(http.CloseNotifier); ok {
		v.CloseNotify()
	}
	if v, ok := txn.(http.Flusher); ok {
		v.Flush()
	}
	if v, ok := txn.(http.Hijacker); ok {
		v.Hijack()
	}
	if v, ok := txn.(io.ReaderFrom); ok {
		v.ReadFrom(nil)
	}
	if !rw.hijackCalled ||
		!rw.readFromCalled ||
		!rw.flushCalled ||
		!rw.closeNotifyCalled {
		t.Error("wrong methods called", rw)
	}
}

func TestTransactionNoExtraMethods(t *testing.T) {
	app := testApp(nil, nil, t)
	rw := &rwNoExtraMethods{}
	txn := app.StartTransaction("hello", rw, nil)
	if _, ok := txn.(http.CloseNotifier); ok {
		t.Error("unexpected CloseNotifier method")
	}
	if _, ok := txn.(http.Flusher); ok {
		t.Error("unexpected Flusher method")
	}
	if _, ok := txn.(http.Hijacker); ok {
		t.Error("unexpected Hijacker method")
	}
	if _, ok := txn.(io.ReaderFrom); ok {
		t.Error("unexpected ReaderFrom method")
	}
}

func TestTransactionTwoExtraMethods(t *testing.T) {
	app := testApp(nil, nil, t)
	rw := &rwTwoExtraMethods{}
	txn := app.StartTransaction("hello", rw, nil)
	if _, ok := txn.(http.CloseNotifier); ok {
		t.Error("unexpected CloseNotifier method")
	}
	if v, ok := txn.(http.Flusher); ok {
		v.Flush()
	}
	if v, ok := txn.(http.Hijacker); ok {
		v.Hijack()
	}
	if _, ok := txn.(io.ReaderFrom); ok {
		t.Error("unexpected ReaderFrom method")
	}
	if !rw.hijackCalled ||
		rw.readFromCalled ||
		!rw.flushCalled ||
		rw.closeNotifyCalled {
		t.Error("wrong methods called", rw)
	}
}