@@ -519,3 +519,74 @@ func TestOptValues(t *testing.T) {
519519 }
520520 })
521521}
522+
523+ // TestCreateSchemaWithContext tests that CreateSchema correctly matches
524+ // context-prefixed subjects returned by the registry.
525+ func TestCreateSchemaWithContext (t * testing.T ) {
526+ dummySchema := Schema {Schema : `{"type":"string"}` }
527+
528+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
529+ path := r .URL .EscapedPath ()
530+ var output any
531+ switch {
532+ case path == "/contexts/.myctx/subjects/foo/versions" && r .Method == http .MethodPost :
533+ output = map [string ]int {"id" : 1 }
534+ case path == "/contexts/.myctx/schemas/ids/1/versions" && r .Method == http .MethodGet :
535+ // The real registry returns subjects with context prefix.
536+ output = []map [string ]any {{"subject" : ":.myctx:foo" , "version" : 1 }}
537+ case path == "/contexts/.myctx/subjects/:.myctx:foo/versions/1" && r .Method == http .MethodGet :
538+ output = SubjectSchema {
539+ Subject : ":.myctx:foo" ,
540+ Version : 1 ,
541+ ID : 1 ,
542+ Schema : dummySchema ,
543+ }
544+ default :
545+ http .Error (w , fmt .Sprintf ("path not found: %s (method: %s)" , path , r .Method ), http .StatusNotFound )
546+ return
547+ }
548+ b , err := json .Marshal (output )
549+ if err != nil {
550+ http .Error (w , err .Error (), http .StatusInternalServerError )
551+ return
552+ }
553+ w .Write (b )
554+ }))
555+ t .Cleanup (ts .Close )
556+
557+ ctx := context .Background ()
558+
559+ t .Run ("WithSchemaContext" , func (t * testing.T ) {
560+ c , err := NewClient (URLs (ts .URL ))
561+ if err != nil {
562+ t .Fatalf ("unable to create client: %s" , err )
563+ }
564+ ss , err := c .CreateSchema (WithSchemaContext (ctx , ".myctx" ), "foo" , dummySchema )
565+ if err != nil {
566+ t .Fatalf ("CreateSchema with WithSchemaContext failed: %s" , err )
567+ }
568+ if ss .Subject != ":.myctx:foo" {
569+ t .Errorf ("expected subject %q, got %q" , ":.myctx:foo" , ss .Subject )
570+ }
571+ if ss .ID != 1 {
572+ t .Errorf ("expected ID 1, got %d" , ss .ID )
573+ }
574+ })
575+
576+ t .Run ("DefaultSchemaContext" , func (t * testing.T ) {
577+ c , err := NewClient (URLs (ts .URL ), DefaultSchemaContext (".myctx" ))
578+ if err != nil {
579+ t .Fatalf ("unable to create client: %s" , err )
580+ }
581+ ss , err := c .CreateSchema (ctx , "foo" , dummySchema )
582+ if err != nil {
583+ t .Fatalf ("CreateSchema with DefaultSchemaContext failed: %s" , err )
584+ }
585+ if ss .Subject != ":.myctx:foo" {
586+ t .Errorf ("expected subject %q, got %q" , ":.myctx:foo" , ss .Subject )
587+ }
588+ if ss .ID != 1 {
589+ t .Errorf ("expected ID 1, got %d" , ss .ID )
590+ }
591+ })
592+ }
0 commit comments