-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
303 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package gapi | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func TestGrpcLogger(t *testing.T) { | ||
// Mock gRPC handler function | ||
mockHandler := func(ctx context.Context, req interface{}) (interface{}, error) { | ||
return nil, nil | ||
} | ||
|
||
// Mock gRPC UnaryServerInfo | ||
mockInfo := &grpc.UnaryServerInfo{ | ||
FullMethod: "/example.Service/Method", | ||
} | ||
|
||
// Execute GrpcLogger | ||
_, err := GrpcLogger(context.Background(), nil, mockInfo, mockHandler) | ||
require.NoError(t, err) | ||
|
||
// Capture logs during the test | ||
logs := make([]string, 0) | ||
captureLogs(func() { | ||
// Execute GrpcLogger | ||
_, err := GrpcLogger(context.Background(), nil, mockInfo, mockHandler) | ||
require.NoError(t, err) | ||
|
||
}, func(log string) { | ||
logs = append(logs, log) | ||
}) | ||
|
||
// Verify the log output | ||
assert.Contains(t, logs[0], "received grpc req") | ||
assert.Contains(t, logs[0], "protocol=grpc") | ||
assert.Contains(t, logs[0], "method=/example.Service/Method") | ||
assert.Contains(t, logs[0], "status_code=0") // The default value for codes.OK | ||
assert.Contains(t, logs[0], "status_text=OK") | ||
assert.Contains(t, logs[0], "duration=") | ||
|
||
} | ||
|
||
func TestHttpLogger(t *testing.T) { | ||
// Mock HTTP handler function | ||
mockHandler := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
res.WriteHeader(http.StatusOK) | ||
res.Write([]byte("OK")) | ||
}) | ||
|
||
// Create a test HTTP request | ||
req := httptest.NewRequest("GET", "/test", nil) | ||
rec := httptest.NewRecorder() | ||
|
||
// Capture logs during the test | ||
logs := make([]string, 0) | ||
captureLogs(func() { | ||
// Execute HttpLogger | ||
HttpLogger(mockHandler).ServeHTTP(rec, req) | ||
}, func(log string) { | ||
logs = append(logs, log) | ||
}) | ||
|
||
// Verify the log output | ||
assert.Contains(t, logs[0], "received http req") | ||
assert.Contains(t, logs[0], "status_code=200") | ||
assert.Contains(t, logs[0], "status_text=OK") | ||
} | ||
|
||
func TestHttpLoggerErr(t *testing.T) { | ||
// Mock HTTP handler function | ||
mockHandler := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
res.WriteHeader(http.StatusInternalServerError) | ||
res.Write([]byte("Error")) | ||
}) | ||
|
||
// Create a test HTTP request | ||
req := httptest.NewRequest("GET", "/test", nil) | ||
rec := httptest.NewRecorder() | ||
|
||
// Capture logs during the test | ||
logs := make([]string, 0) | ||
captureLogs(func() { | ||
// Execute HttpLogger | ||
HttpLogger(mockHandler).ServeHTTP(rec, req) | ||
}, func(log string) { | ||
logs = append(logs, log) | ||
}) | ||
|
||
// Verify the log output using substring matching | ||
assert.Contains(t, logs[0], "received http req") | ||
assert.Contains(t, logs[0], "status_code=500") | ||
assert.Contains(t, logs[0], "status_text=\"Internal Server Error\"") | ||
assert.Contains(t, logs[0], "body=Error") | ||
} | ||
|
||
func TestResponseRecorder(t *testing.T) { | ||
// Mock ResponseWriter | ||
mockResponseWriter := httptest.NewRecorder() | ||
|
||
// Create a ResponseRecorder | ||
rec := &ResponseRecorder{ | ||
ResponseWriter: mockResponseWriter, | ||
StatusCode: http.StatusOK, | ||
} | ||
|
||
// Write some data to ResponseRecorder | ||
rec.Write([]byte("Test Body")) | ||
|
||
// Verify the StatusCode | ||
require.Equal(t, rec.StatusCode, http.StatusOK) | ||
|
||
// Verify the captured body | ||
require.Equal(t, string(rec.Body), "Test Body") | ||
|
||
} | ||
|
||
// captureLogs captures logs produced during the execution of a function. | ||
func captureLogs(fn func(), logCallback func(string)) { | ||
originalOutput := log.Writer() | ||
defer func() { log.SetOutput(originalOutput) }() | ||
|
||
r, w, err := os.Pipe() | ||
if err != nil { | ||
panic(err) | ||
} | ||
log.SetOutput(w) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
|
||
go func() { | ||
defer wg.Done() | ||
var buf bytes.Buffer | ||
io.Copy(&buf, r) | ||
logCallback(buf.String()) | ||
}() | ||
|
||
fn() | ||
|
||
w.Close() | ||
wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"google.golang.org/grpc/metadata" | ||
"google.golang.org/grpc/peer" | ||
) | ||
|
||
func TestExtractMetadata(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
headers metadata.MD | ||
peerAddr net.IP | ||
expected Metadata | ||
}{ | ||
{ | ||
name: "ExtractMetadataFromGateway", | ||
headers: metadata.Pairs( | ||
grpcGatewayUserAgentHeader, "grpc-gateway-user-agent-value", | ||
xForwardedForHeader, "127.0.0.1", | ||
), | ||
expected: Metadata{ | ||
UserAgent: "grpc-gateway-user-agent-value", | ||
ClientIP: "127.0.0.1", | ||
}, | ||
}, | ||
{ | ||
name: "ExtractMetadataFromGrpc", | ||
headers: metadata.Pairs( | ||
userAgentHeader, "user-agent-value", | ||
), | ||
peerAddr: net.ParseIP("127.0.0.1"), | ||
expected: Metadata{ | ||
UserAgent: "user-agent-value", | ||
ClientIP: "127.0.0.1", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
server := &Server{} | ||
ctx := context.Background() | ||
|
||
// Set metadata in context | ||
if len(tc.headers) > 0 { | ||
ctx = metadata.NewIncomingContext(ctx, tc.headers) | ||
} | ||
|
||
// Set peer information in context | ||
if tc.peerAddr != nil { | ||
p := &peer.Peer{ | ||
Addr: &net.IPAddr{ | ||
IP: tc.peerAddr, | ||
}, | ||
} | ||
ctx = peer.NewContext(ctx, p) | ||
} | ||
|
||
// Execute the extractMetadata method | ||
result := server.extractMetadata(ctx) | ||
|
||
// Verify the extracted metadata | ||
assert.Equal(t, tc.expected.UserAgent, result.UserAgent) | ||
assert.Equal(t, tc.expected.ClientIP, result.ClientIP) | ||
}) | ||
} | ||
} |
Oops, something went wrong.