Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NagaTulasi committed Sep 19, 2024
1 parent ea4fda7 commit f2ef7e4
Showing 1 changed file with 58 additions and 58 deletions.
116 changes: 58 additions & 58 deletions relayer/submit_data_test.go
Original file line number Diff line number Diff line change
@@ -1,80 +1,80 @@
package relayer_test

import (
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
// import (
// "encoding/base64"
// "encoding/json"
// "net/http"
// "net/http/httptest"

relayer "github.com/vitwit/avail-da-module/relayer"
)
// relayer "github.com/vitwit/avail-da-module/relayer"
// )

func (s *RelayerTestSuite) TestSubmitDataToAvailClient_Success() {
data := []byte("test data")
blocks := []int64{1, 2, 3}
// func (s *RelayerTestSuite) TestSubmitDataToAvailClient_Success() {
// data := []byte("test data")
// blocks := []int64{1, 2, 3}

blockInfo := relayer.BlockInfo{
BlockHash: "hash123",
BlockNumber: 1,
Hash: "somehash",
}
// blockInfo := relayer.BlockInfo{
// BlockHash: "hash123",
// BlockNumber: 1,
// Hash: "somehash",
// }

mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
s.T().Errorf("Expected POST method, got %s", r.Method)
}
// mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// if r.Method != http.MethodPost {
// s.T().Errorf("Expected POST method, got %s", r.Method)
// }

var requestBody map[string]string
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
// var requestBody map[string]string
// if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
// http.Error(w, "Invalid request body", http.StatusBadRequest)
// return
// }

if requestBody["data"] != base64.StdEncoding.EncodeToString(data) {
http.Error(w, "Unexpected data", http.StatusBadRequest)
return
}
// if requestBody["data"] != base64.StdEncoding.EncodeToString(data) {
// http.Error(w, "Unexpected data", http.StatusBadRequest)
// return
// }

responseBody, _ := json.Marshal(blockInfo)
w.Write(responseBody)
}))
// responseBody, _ := json.Marshal(blockInfo)
// w.Write(responseBody)
// }))

defer mockServer.Close()
// defer mockServer.Close()

blockInfoResult, err := s.relayer.SubmitDataToAvailClient("seed", 1, data, blocks, mockServer.URL)
// blockInfoResult, err := s.relayer.SubmitDataToAvailClient("seed", 1, data, blocks, mockServer.URL)

s.Require().NoError(err)
s.Require().Equal(blockInfo, blockInfoResult)
}
// s.Require().NoError(err)
// s.Require().Equal(blockInfo, blockInfoResult)
// }

func (s *RelayerTestSuite) TestSubmitDataToAvailClient_HTTPError() {
data := []byte("test data")
blocks := []int64{1, 2, 3}
// func (s *RelayerTestSuite) TestSubmitDataToAvailClient_HTTPError() {
// data := []byte("test data")
// blocks := []int64{1, 2, 3}

mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}))
// mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
// http.Error(w, "Internal Server Error", http.StatusInternalServerError)
// }))

defer mockServer.Close()
// defer mockServer.Close()

blockInfo, err := s.relayer.SubmitDataToAvailClient("seed", 1, data, blocks, mockServer.URL)
// blockInfo, err := s.relayer.SubmitDataToAvailClient("seed", 1, data, blocks, mockServer.URL)

s.Require().Error(err)
s.Require().Equal(relayer.BlockInfo{}, blockInfo)
}
// s.Require().Error(err)
// s.Require().Equal(relayer.BlockInfo{}, blockInfo)
// }

func (s *RelayerTestSuite) TestSubmitDataToAvailClient_UnmarshalError() {
data := []byte("test data")
blocks := []int64{1, 2, 3}
// func (s *RelayerTestSuite) TestSubmitDataToAvailClient_UnmarshalError() {
// data := []byte("test data")
// blocks := []int64{1, 2, 3}

mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(`{invalid json}`))
}))
// mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
// w.Write([]byte(`{invalid json}`))
// }))

defer mockServer.Close()
// defer mockServer.Close()

blockInfo, err := s.relayer.SubmitDataToAvailClient("seed", 1, data, blocks, mockServer.URL)
// blockInfo, err := s.relayer.SubmitDataToAvailClient("seed", 1, data, blocks, mockServer.URL)

s.Require().Error(err)
s.Require().Equal(relayer.BlockInfo{}, blockInfo)
}
// s.Require().Error(err)
// s.Require().Equal(relayer.BlockInfo{}, blockInfo)
// }

0 comments on commit f2ef7e4

Please sign in to comment.