-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is meant to mirror HTTP headers as much as possible for interop not only between our clients but HTTP clients in general. I actually used the http.Header directly, so we get canonical names, etc. I thought about adding in a way to add the data field to a message such that you could detect mime-type and content-length etc. But Go's builtin type detection is not that good, so stuck with basics. Signed-off-by: Derek Collison <derek@nats.io>
- Loading branch information
1 parent
076024f
commit 965d31d
Showing
4 changed files
with
207 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
module github.com/nats-io/nats.go | ||
|
||
go 1.14 | ||
|
||
require ( | ||
github.com/golang/protobuf v1.4.0 | ||
github.com/nats-io/jwt v0.3.2 | ||
github.com/nats-io/nats-server/v2 v2.1.6 | ||
github.com/nats-io/nkeys v0.1.4 | ||
github.com/nats-io/nuid v1.0.1 | ||
google.golang.org/protobuf v1.21.0 | ||
) |
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
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,93 @@ | ||
// Copyright 2020 The NATS Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/nats-io/nats.go" | ||
) | ||
|
||
func TestBasicHeaders(t *testing.T) { | ||
s := RunServerOnPort(-1) | ||
defer s.Shutdown() | ||
|
||
nc, err := nats.Connect(s.ClientURL()) | ||
if err != nil { | ||
t.Fatalf("Error connecting to server: %v", err) | ||
} | ||
defer nc.Close() | ||
|
||
subject := "headers.test" | ||
sub, err := nc.SubscribeSync(subject) | ||
if err != nil { | ||
t.Fatalf("Could not subscribe to %q: %v", subject, err) | ||
} | ||
defer sub.Unsubscribe() | ||
|
||
m := nats.NewMsg(subject) | ||
m.Header.Add("Accept-Encoding", "json") | ||
m.Header.Add("Authorization", "s3cr3t") | ||
m.Data = []byte("Hello Headers!") | ||
|
||
nc.PublishMsg(m) | ||
msg, _ := sub.NextMsg(time.Second) | ||
|
||
// Blank out the sub since its not present in the original. | ||
msg.Sub = nil | ||
if !reflect.DeepEqual(m, msg) { | ||
t.Fatalf("Messages did not match! \n%+v\n%+v\n", m, msg) | ||
} | ||
} | ||
|
||
// Make sure we do the right thing with a badly encoded header msg. | ||
func TestBadHeaderMsg(t *testing.T) { | ||
s := RunServerOnPort(-1) | ||
defer s.Shutdown() | ||
|
||
ech := make(chan error) | ||
|
||
nc, err := nats.Connect(s.ClientURL(), nats.ErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, err error) { | ||
ech <- err | ||
})) | ||
if err != nil { | ||
t.Fatalf("Error connecting to server: %v", err) | ||
} | ||
defer nc.Close() | ||
|
||
subject := "headers.test" | ||
sub, err := nc.SubscribeSync(subject) | ||
if err != nil { | ||
t.Fatalf("Could not subscribe to %q: %v", subject, err) | ||
} | ||
defer sub.Unsubscribe() | ||
|
||
bad := []byte("⚡NATS⚡/0.1\r\nAccept-Encoding: json\r\nAutho Bad Headers!") | ||
|
||
nc.Publish(subject, bad) | ||
|
||
tm := time.NewTimer(5 * time.Second) | ||
defer tm.Stop() | ||
|
||
select { | ||
case err := <-ech: | ||
if err != nats.ErrBadHeaderMsg { | ||
t.Fatalf("Did not get the error we wanted, got %v", err) | ||
} | ||
case <-tm.C: | ||
t.Fatalf("Did not receive an error") | ||
} | ||
} |