Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ type Interface interface {
// CloseReason returns the metadata explaining why a device was closed. If this device
// is not closed, this method's return is undefined.
CloseReason() CloseReason

// IntermediateContext returns any additional information if sent by the device.
IntermediateContext() string
}

// device is the internal Interface implementation. This type holds the internal
Expand All @@ -124,6 +127,8 @@ type device struct {
metadata *Metadata

closeReason atomic.Value

intermediateContext string
}

type deviceOptions struct {
Expand Down Expand Up @@ -320,3 +325,7 @@ func (d *device) CloseReason() CloseReason {

return CloseReason{}
}

func (d *device) IntermediateContext() string {
return d.intermediateContext
}
72 changes: 72 additions & 0 deletions device/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,75 @@ func TestDevice(t *testing.T) {
assert.Error(err)
}
}

func TestDevice_IntermediateContext(t *testing.T) {
tests := []struct {
name string
intermediateContext string
expectedIntermediateContext string
}{
{
name: "empty intermediate context",
intermediateContext: "",
expectedIntermediateContext: "",
},
{
name: "non-empty intermediate context",
intermediateContext: "some-context-value",
expectedIntermediateContext: "some-context-value",
},
{
name: "intermediate context with special characters",
intermediateContext: "context/with/special-chars_123",
expectedIntermediateContext: "context/with/special-chars_123",
},
{
name: "intermediate context with JSON-like content",
intermediateContext: `{"key": "value", "nested": {"data": 123}}`,
expectedIntermediateContext: `{"key": "value", "nested": {"data": 123}}`,
},
{
name: "intermediate context with whitespace",
intermediateContext: " spaced context ",
expectedIntermediateContext: " spaced context ",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

device := newDevice(deviceOptions{
ID: ID("test-device"),
QueueSize: 10,
Logger: sallust.Default(),
Metadata: new(Metadata),
})
require.NotNil(device)

// Set the intermediateContext field directly since it's an internal field
device.intermediateContext = tc.intermediateContext

assert.Equal(tc.expectedIntermediateContext, device.IntermediateContext())
})
}
}

func TestDevice_IntermediateContext_Default(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

// Create a device without setting intermediateContext
device := newDevice(deviceOptions{
ID: ID("test-device"),
QueueSize: 10,
Logger: sallust.Default(),
Metadata: new(Metadata),
})
require.NotNil(device)

// Default value should be empty string
assert.Empty(device.IntermediateContext())
assert.Equal("", device.IntermediateContext())
}
1 change: 1 addition & 0 deletions device/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func (m *manager) Connect(response http.ResponseWriter, request *http.Request, r
Metadata: metadata,
Logger: m.logger,
})
d.intermediateContext = request.Header.Get("X-Intermediate-Context")

if allow, matchResults := m.filter.AllowConnection(d); !allow {
d.logger.Info("filter match found", zap.String("location", matchResults.Location), zap.String("key", matchResults.Key))
Expand Down
4 changes: 4 additions & 0 deletions device/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ func (m *MockDevice) CloseReason() CloseReason {
return first
}

func (m *MockDevice) IntermediateContext() string {
return ""
}

func (m *MockDevice) Send(request *Request) (*Response, error) {
// nolint: typecheck
arguments := m.Called(request)
Expand Down
Loading