├── .gitignore ├── AsciiBinaryOctalConverter.code-workspace ├── Readme.md ├── .github └── workflows │ ├── gifs.yml │ ├── test.yml │ └── release.yml ├── go.mod ├── converters ├── converters_test.go └── converters.go ├── go.sum ├── main.go └── main_test.go /.gitignore: -------------------------------------------------------------------------------- 1 | notes.txt -------------------------------------------------------------------------------- /AsciiBinaryOctalConverter.code-workspace: -------------------------------------------------------------------------------- 1 | { 2 | "folders": [ 3 | { 4 | "path": "." 5 | }, 6 | { 7 | "path": "../.git" 8 | } 9 | ], 10 | "settings": { 11 | "liveServer.settings.multiRootWorkspaceName": "AsciiBinaryOctalConverter" 12 | } 13 | } -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Ascii-Binary-Octal Converter 2 | 3 | ## Testing 4 | 5 | ### How to see coverage reports? 6 | 7 | * Go to the package to be tested 8 | * `go test -coverprofile=coverage.out` 9 | * `go tool cover -html=coverage.out -o coverage.html` 10 | 11 | ### This repo is powered by Github Actions!!! 12 | -------------------------------------------------------------------------------- /.github/workflows/gifs.yml: -------------------------------------------------------------------------------- 1 | name: Dogs 🐕 2 | 3 | on: 4 | pull_request_target: 5 | types: 6 | - opened 7 | - reopened 8 | 9 | jobs: 10 | aCatForCreatingThePullRequest: 11 | name: A dog for your effort! 12 | runs-on: ubuntu-latest 13 | permissions: write-all 14 | steps: 15 | - uses: stanleynguyen/action-dogs@v1 16 | with: 17 | github-token: ${{ secrets.GITHUB_TOKEN }} 18 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module api 2 | 3 | go 1.19 4 | 5 | require github.com/gofiber/fiber/v2 v2.48.0 6 | 7 | require ( 8 | github.com/andybalholm/brotli v1.0.5 // indirect 9 | github.com/google/uuid v1.3.0 // indirect 10 | github.com/klauspost/compress v1.16.3 // indirect 11 | github.com/mattn/go-colorable v0.1.13 // indirect 12 | github.com/mattn/go-isatty v0.0.19 // indirect 13 | github.com/mattn/go-runewidth v0.0.14 // indirect 14 | github.com/rivo/uniseg v0.4.3 // indirect 15 | github.com/valyala/bytebufferpool v1.0.0 // indirect 16 | github.com/valyala/fasthttp v1.48.0 // indirect 17 | github.com/valyala/tcplisten v1.0.0 // indirect 18 | golang.org/x/sys v0.10.0 // indirect 19 | ) 20 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | push: 5 | branches: [Converter] 6 | 7 | jobs: 8 | test: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - name: Checkout code 13 | uses: actions/checkout@v2 14 | 15 | - name: Install dependencies 16 | run: go mod download 17 | 18 | - name: Test with Go 19 | run: go test -coverprofile=coverage.out 20 | 21 | - name: Produce coverage report 22 | run: go tool cover -html=coverage.out -o coverage.html 23 | 24 | - name: Archive code coverage results 25 | uses: actions/upload-artifact@v3 26 | with: 27 | name: code-coverage-report 28 | path: coverage.html 29 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Create Release 2 | 3 | on: 4 | push: 5 | branches: [Converter] 6 | 7 | jobs: 8 | build: 9 | name: Create Release 10 | runs-on: ubuntu-latest 11 | permissions: write-all 12 | steps: 13 | - name: Checkout code 14 | uses: actions/checkout@v2 15 | - name: Build 16 | run: go build -o api 17 | - name: Set up Python 18 | uses: actions/setup-python@v2 19 | with: 20 | python-version: '3.x' 21 | - name: Generate unique tag 22 | id: gen_tag 23 | run: echo ::set-output name=tag::$(date "+%m.%d-%H.%M") 24 | - name: Create Release 25 | id: create_release 26 | uses: actions/create-release@v1 27 | env: 28 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 29 | with: 30 | tag_name: ${{ steps.gen_tag.outputs.tag }} 31 | release_name: Release ${{ steps.gen_tag.outputs.tag }} 32 | draft: false 33 | prerelease: false 34 | - name: Upload Release Asset 35 | uses: actions/upload-release-asset@v1 36 | env: 37 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 38 | with: 39 | upload_url: ${{ steps.create_release.outputs.upload_url }} 40 | asset_path: ./api 41 | asset_name: api.exe 42 | asset_content_type: application/octet-stream 43 | -------------------------------------------------------------------------------- /converters/converters_test.go: -------------------------------------------------------------------------------- 1 | package converters 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestAsciiToBinary(t *testing.T) { 8 | testcases := []struct { 9 | in, want string 10 | }{ 11 | {"dilara", "01100100 01101001 01101100 01100001 01110010 01100001"}, 12 | } 13 | for _, tc := range testcases { 14 | res := AsciiToBinary(tc.in) 15 | if res != tc.want { 16 | t.Errorf("sonuç: %s, olması gereken: %s", res, tc.want) 17 | } 18 | } 19 | } 20 | 21 | func TestAsciiToOctal(t *testing.T) { 22 | testcases := []struct { 23 | in, want string 24 | }{ 25 | {"dilara", "144 151 154 141 162 141"}, 26 | {"dilara0", "144 151 154 141 162 141 060"}, 27 | } 28 | for _, tc := range testcases { 29 | res := AsciiToOctal(tc.in) 30 | if res != tc.want { 31 | t.Errorf("sonuç: %s, olması gereken: %s", res, tc.want) 32 | } 33 | } 34 | } 35 | 36 | func TestBinaryToAscii(t *testing.T) { 37 | testcases := []struct { 38 | in, want string 39 | }{ 40 | {"01100100 01101001 01101100 01100001 01110010 01100001", "d i l a r a"}, 41 | {"1010101", "U"}, 42 | } 43 | for _, tc := range testcases { 44 | res := BinaryToAscii(tc.in) 45 | if res != tc.want { 46 | t.Errorf("sonuç: %s, olması gereken: %s", res, tc.want) 47 | } 48 | } 49 | } 50 | func TestBinaryToOctal(t *testing.T) { 51 | testcases := []struct { 52 | in, want string 53 | }{ 54 | {"01100100 01101001 01101100 01100001 01110010 01100001", "144 151 154 141 162 141"}, 55 | {"1010101", "125"}, 56 | } 57 | for _, tc := range testcases { 58 | res := BinaryToOctal(tc.in) 59 | if res != tc.want { 60 | t.Errorf("sonuç: %s, olması gereken: %s", res, tc.want) 61 | } 62 | } 63 | } 64 | 65 | func TestOctaltoAscii(t *testing.T) { 66 | testcases := []struct { 67 | in, want string 68 | }{ 69 | {"144 151 154 141 162 141", "d i l a r a"}, 70 | {"60", "0"}, 71 | } 72 | for _, tc := range testcases { 73 | res := OctalToAscii(tc.in) 74 | if res != tc.want { 75 | t.Errorf("sonuç: %s, olması gereken: %s", res, tc.want) 76 | } 77 | } 78 | } 79 | 80 | func TestOctaltoBinary(t *testing.T) { 81 | testcases := []struct { 82 | in, want string 83 | }{ 84 | {"144 151 154 141 162 141", "01100100 01101001 01101100 01100001 01110010 01100001"}, 85 | {"60", "00110000"}, 86 | } 87 | for _, tc := range testcases { 88 | res := OctalToBinary(tc.in) 89 | if res != tc.want { 90 | t.Errorf("sonuç: %s, olması gereken: %s", res, tc.want) 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= 2 | github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= 3 | github.com/gofiber/fiber/v2 v2.48.0 h1:cRVMCb9aUJDsyHxGFLwz/sGzDggdailZZyptU9F9cU0= 4 | github.com/gofiber/fiber/v2 v2.48.0/go.mod h1:xqJgfqrc23FJuqGOW6DVgi3HyZEm2Mn9pRqUb2kHSX8= 5 | github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= 6 | github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 7 | github.com/klauspost/compress v1.16.3 h1:XuJt9zzcnaz6a16/OU53ZjWp/v7/42WcR5t2a0PcNQY= 8 | github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= 9 | github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= 10 | github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= 11 | github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= 12 | github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= 13 | github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 14 | github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= 15 | github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= 16 | github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= 17 | github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw= 18 | github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= 19 | github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= 20 | github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= 21 | github.com/valyala/fasthttp v1.48.0 h1:oJWvHb9BIZToTQS3MuQ2R3bJZiNSa2KiNdeI8A+79Tc= 22 | github.com/valyala/fasthttp v1.48.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA= 23 | github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= 24 | github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= 25 | golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 26 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 27 | golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= 28 | golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 29 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "api/converters" 5 | "encoding/json" 6 | "log" 7 | 8 | "github.com/gofiber/fiber/v2" 9 | "github.com/gofiber/fiber/v2/middleware/cors" 10 | ) 11 | 12 | func main() { 13 | 14 | app := Setup() 15 | 16 | log.Fatal(app.Listen("127.0.0.1:8080")) // I changed this part to try to ping in my ServerManagement project. 17 | } 18 | 19 | func Setup() *fiber.App { 20 | 21 | app := fiber.New() 22 | 23 | app.Use(cors.New()) 24 | 25 | app.Post("/convert", func(c *fiber.Ctx) error { 26 | 27 | type Result struct { 28 | Output string `json:"message"` 29 | } 30 | 31 | type RequestBody struct { 32 | Value string `json:"value"` 33 | SourceType string `json:"sourceType"` 34 | DestType string `json:"destType"` 35 | } 36 | 37 | var reqBody RequestBody 38 | 39 | if err := c.BodyParser(&reqBody); err != nil { 40 | return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ 41 | "error": "Bad Request", 42 | }) 43 | } 44 | 45 | if reqBody.SourceType == "ascii" && reqBody.DestType == "binary" { 46 | asciiInput := reqBody.Value 47 | binaryResult := converters.AsciiToBinary(asciiInput) 48 | result := Result{Output: binaryResult} 49 | jsonResult, _ := json.Marshal(result) 50 | 51 | c.Type("application/json") // Type sets the Content-Type HTTP header to the MIME type specified by the file extension. 52 | 53 | c.Send(jsonResult) //Send sets the HTTP response body without copying it 54 | 55 | } else if reqBody.SourceType == "ascii" && reqBody.DestType == "octal" { 56 | asciiInput := reqBody.Value 57 | octalResult := converters.AsciiToOctal(asciiInput) 58 | 59 | result := Result{Output: octalResult} 60 | jsonResult, _ := json.Marshal(result) 61 | 62 | c.Type("application/json") 63 | 64 | c.Send(jsonResult) 65 | 66 | } else if reqBody.SourceType == "binary" && reqBody.DestType == "ascii" { 67 | binaryInput := reqBody.Value 68 | asciiResult := converters.BinaryToAscii(binaryInput) 69 | 70 | result := Result{Output: asciiResult} 71 | jsonResult, _ := json.Marshal(result) 72 | 73 | c.Type("application/json") 74 | 75 | c.Send(jsonResult) 76 | 77 | } else if reqBody.SourceType == "binary" && reqBody.DestType == "octal" { 78 | binaryInput := reqBody.Value 79 | octalResult := converters.BinaryToOctal(binaryInput) 80 | 81 | result := Result{Output: octalResult} 82 | jsonResult, _ := json.Marshal(result) 83 | 84 | c.Type("application/json") 85 | 86 | c.Send(jsonResult) 87 | 88 | } else if reqBody.SourceType == "octal" && reqBody.DestType == "ascii" { 89 | octalInput := reqBody.Value 90 | asciiResult := converters.OctalToAscii(octalInput) 91 | 92 | result := Result{Output: asciiResult} 93 | jsonResult, _ := json.Marshal(result) 94 | 95 | c.Type("application/json") 96 | 97 | c.Send(jsonResult) 98 | 99 | } else if reqBody.SourceType == "octal" && reqBody.DestType == "binary" { 100 | octalInput := reqBody.Value 101 | binaryResult := converters.OctalToBinary(octalInput) 102 | 103 | result := Result{Output: binaryResult} 104 | jsonResult, _ := json.Marshal(result) 105 | 106 | c.Type("application/json") 107 | 108 | c.Send(jsonResult) 109 | 110 | } 111 | 112 | return nil 113 | }) 114 | return app 115 | } 116 | -------------------------------------------------------------------------------- /converters/converters.go: -------------------------------------------------------------------------------- 1 | package converters 2 | 3 | import ( 4 | "strconv" 5 | "strings" 6 | ) 7 | 8 | func AsciiToBinary(input string) string { // ASCII to Binary 9 | 10 | output := "" 11 | 12 | for i := 0; i < len([]rune(input)); i++ { 13 | 14 | binary := strconv.FormatInt(int64(input[i]), 2) // ASCII kodunu ikili sayıya çeviriyoruz 15 | 16 | if len(binary) < 8 { // Eğer sayının uzunluğu 8 bit değilse başına sıfır ekliyoruz 17 | for i := len(binary); i < 8; i++ { 18 | binary = "0" + binary 19 | } 20 | } 21 | 22 | if i < len([]rune(input))-1 { 23 | output += binary + " " 24 | } else { 25 | output += binary 26 | } 27 | } 28 | 29 | return output 30 | } 31 | 32 | func AsciiToOctal(input string) string { // ASCII to Octal 33 | 34 | output := "" 35 | 36 | for i := 0; i < len([]rune(input)); i++ { 37 | 38 | octal := strconv.FormatInt(int64(input[i]), 8) // ASCII kodunu sekizli sayıya çeviriyoruz 39 | 40 | for len(octal) < 3 { // Eğer sayının uzunluğu 2 basamak değilse başına sıfır ekliyoruz 41 | octal = "0" + octal 42 | } 43 | 44 | if i < len([]rune(input))-1 { 45 | output += octal + " " 46 | } else { 47 | output += octal 48 | } 49 | } 50 | 51 | return output 52 | } 53 | 54 | func BinaryToAscii(input string) string { // Binary to ASCII 55 | 56 | output := "" 57 | 58 | unwanted := " " 59 | input = strings.Map(func(r rune) rune { 60 | if strings.ContainsRune(unwanted, r) { 61 | return -1 62 | } 63 | return r 64 | }, input) 65 | 66 | for i := 0; i < len(input); i += 8 { // Her 8 karakter için ASCII karakterlerini birleştiriyoruz 67 | 68 | if i+8 > len(input) { 69 | x := strings.Repeat("0", i+8-len(input)) 70 | input = x + input 71 | } 72 | 73 | binary := input[i : i+8] // 8 bitlik ikili sayıyı ASCII karakterine dönüştürüyoruz ex.U -> 01010101 74 | ascii, _ := strconv.ParseInt(binary, 2, 64) //(01010101=U) (U=85 in 10) 75 | 76 | if i+8 == (len([]rune(input))) { 77 | output += string(rune(ascii)) 78 | } else { 79 | output += string(rune(ascii)) + " " 80 | } 81 | 82 | } 83 | 84 | return output 85 | } 86 | 87 | func BinaryToOctal(input string) string { // Binary to Octal 88 | output := "" 89 | 90 | unwanted := " " 91 | input = strings.Map(func(r rune) rune { 92 | if strings.ContainsRune(unwanted, r) { 93 | return -1 94 | } 95 | return r 96 | }, input) 97 | 98 | for i := 0; i < len(input); i += 8 { 99 | 100 | if i+8 > len(input) { 101 | x := strings.Repeat("0", i+8-len(input)) 102 | input = x + input 103 | } 104 | 105 | binary := input[i : i+8] 106 | octal, _ := strconv.ParseInt(binary, 2, 64) 107 | 108 | if i+8 == (len([]rune(input))) { 109 | output += strconv.FormatInt(octal, 8) 110 | } else { 111 | output += strconv.FormatInt(octal, 8) + " " 112 | } 113 | 114 | } 115 | 116 | return output 117 | } 118 | 119 | func OctalToAscii(input string) string { // Octal to ASCII 120 | output := "" 121 | 122 | unwanted := " " 123 | input = strings.Map(func(r rune) rune { 124 | if strings.ContainsRune(unwanted, r) { 125 | return -1 126 | } 127 | return r 128 | }, input) 129 | 130 | for i := 0; i < len(input); i += 3 { // Her 3 karakter için ASCII karakterlerini birleştiriyoruz 131 | 132 | if i+3 > len(input) { // Eğer son 3 karakterin uzunluğu 2'den az ise başına sıfır ekliyoruz 133 | x := strings.Repeat("0", i+3-len(input)) 134 | input = x + input 135 | } 136 | 137 | octal := input[i : i+3] // 3 basamaklı sekizli sayıyı ASCII karakterine dönüştürüyoruz 138 | ascii, _ := strconv.ParseInt(octal, 8, 64) 139 | 140 | if i+3 == (len([]rune(input))) { 141 | output += string(rune(ascii)) 142 | } else { 143 | output += string(rune(ascii)) + " " 144 | } 145 | 146 | } 147 | 148 | return output 149 | } 150 | 151 | func OctalToBinary(input string) string { // Octal to Binary 152 | output := "" 153 | 154 | unwanted := " " 155 | input = strings.Map(func(r rune) rune { 156 | if strings.ContainsRune(unwanted, r) { 157 | return -1 158 | } 159 | return r 160 | }, input) 161 | 162 | for i := 0; i < len(input); i += 3 { // Her 3 karakter için ASCII karakterlerini birleştiriyoruz 163 | 164 | if i+3 > len(input) { // Eğer son 3 karakterin uzunluğu 2'den az ise başına sıfır ekliyoruz 165 | x := strings.Repeat("0", i+3-len(input)) 166 | input = x + input 167 | } 168 | 169 | octal := input[i : i+3] // 3 basamaklı sekizli sayıyı ASCII karakterine dönüştürüyoruz 170 | ascii, _ := strconv.ParseInt(octal, 8, 64) 171 | for _, c := range string(rune(ascii)) { 172 | 173 | binary := strconv.FormatInt(int64(c), 2) // ASCII kodunu ikili sayıya çeviriyoruz 174 | 175 | if len(binary) < 8 { // Eğer sayının uzunluğu 8 bit değilse başına sıfır ekliyoruz 176 | for i := len(binary); i < 8; i++ { 177 | binary = "0" + binary 178 | } 179 | } 180 | 181 | if i+3 == (len([]rune(input))) { 182 | output += binary // Binary çıktısını birleştiriyoruz 183 | } else { 184 | output += binary + " " // Binary çıktısını birleştiriyoruz 185 | } 186 | 187 | } 188 | } 189 | 190 | return output 191 | } 192 | -------------------------------------------------------------------------------- /main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "io" 7 | "net/http" 8 | "net/http/httptest" 9 | "testing" 10 | "time" 11 | ) 12 | 13 | func TestConvertEndpoint(t *testing.T) { 14 | 15 | app := Setup() // Create a new fiber app 16 | defer app.Shutdown() 17 | 18 | ///////////////////////////////////////////////// ASCİİTOBİNARY 19 | // Create a test request 20 | req := httptest.NewRequest("POST", "/convert", bytes.NewBuffer([]byte(`{ 21 | "value": "dilara", 22 | "sourceType": "ascii", 23 | "destType": "binary" 24 | }`))) 25 | req.Header.Set("Content-Type", "application/json") 26 | 27 | // Perform the request 28 | resp, err := app.Test(req, -1) 29 | if err != nil { 30 | t.Fatal(err) 31 | } 32 | 33 | // Check the response status code 34 | if resp.StatusCode != http.StatusOK { 35 | t.Errorf("Expected status code %d but got %d", http.StatusOK, resp.StatusCode) 36 | } 37 | 38 | // Check the response body 39 | expected := `{"message":"01100100 01101001 01101100 01100001 01110010 01100001"}` 40 | body, err := io.ReadAll(resp.Body) 41 | if err != nil { 42 | t.Fatal(err) 43 | } 44 | if string(body) != expected { 45 | t.Errorf("Expected response body '%s' but got '%s'", expected, string(body)) 46 | } 47 | 48 | //////////////////////////////////////////////////ASCİİTOOCTAL 49 | // Create a test request 50 | req = httptest.NewRequest("POST", "/convert", bytes.NewBuffer([]byte(`{ 51 | "value": "dilara0", 52 | "sourceType": "ascii", 53 | "destType": "octal" 54 | }`))) 55 | req.Header.Set("Content-Type", "application/json") 56 | 57 | // Perform the request 58 | resp, err = app.Test(req, -1) 59 | if err != nil { 60 | t.Fatal(err) 61 | } 62 | 63 | // Check the response status code 64 | if resp.StatusCode != http.StatusOK { 65 | t.Errorf("Expected status code %d but got %d", http.StatusOK, resp.StatusCode) 66 | } 67 | 68 | // Check the response body 69 | expected = `{"message":"144 151 154 141 162 141 060"}` 70 | body, err = io.ReadAll(resp.Body) 71 | if err != nil { 72 | t.Fatal(err) 73 | } 74 | if string(body) != expected { 75 | t.Errorf("Expected response body '%s' but got '%s'", expected, string(body)) 76 | } 77 | 78 | /////////////////////////////////////////////////////////BİNARYTOASCİİ 79 | // Create a test request 80 | req = httptest.NewRequest("POST", "/convert", bytes.NewBuffer([]byte(`{ 81 | "value": "1010101", 82 | "sourceType": "binary", 83 | "destType": "ascii" 84 | }`))) 85 | req.Header.Set("Content-Type", "application/json") 86 | 87 | // Perform the request 88 | resp, err = app.Test(req, -1) 89 | if err != nil { 90 | t.Fatal(err) 91 | } 92 | 93 | // Check the response status code 94 | if resp.StatusCode != http.StatusOK { 95 | t.Errorf("Expected status code %d but got %d", http.StatusOK, resp.StatusCode) 96 | } 97 | 98 | // Check the response body 99 | expected = `{"message":"U"}` 100 | body, err = io.ReadAll(resp.Body) 101 | if err != nil { 102 | t.Fatal(err) 103 | } 104 | if string(body) != expected { 105 | t.Errorf("Expected response body '%s' but got '%s'", expected, string(body)) 106 | } 107 | 108 | ///////////////////////////////////////////////////////BİNARYTOOCTAL 109 | // Create a test request 110 | req = httptest.NewRequest("POST", "/convert", bytes.NewBuffer([]byte(`{ 111 | "value": "1010101", 112 | "sourceType": "binary", 113 | "destType": "octal" 114 | }`))) 115 | req.Header.Set("Content-Type", "application/json") 116 | 117 | // Perform the request 118 | resp, err = app.Test(req, -1) 119 | if err != nil { 120 | t.Fatal(err) 121 | } 122 | 123 | // Check the response status code 124 | if resp.StatusCode != http.StatusOK { 125 | t.Errorf("Expected status code %d but got %d", http.StatusOK, resp.StatusCode) 126 | } 127 | 128 | // Check the response body 129 | expected = `{"message":"125"}` 130 | body, err = io.ReadAll(resp.Body) 131 | if err != nil { 132 | t.Fatal(err) 133 | } 134 | if string(body) != expected { 135 | t.Errorf("Expected response body '%s' but got '%s'", expected, string(body)) 136 | } 137 | 138 | ///////////////////////////////////////////////////////////////////////OCTALTOASCİİ 139 | // Create a test request 140 | req = httptest.NewRequest("POST", "/convert", bytes.NewBuffer([]byte(`{ 141 | "value": "60", 142 | "sourceType": "octal", 143 | "destType": "ascii" 144 | }`))) 145 | req.Header.Set("Content-Type", "application/json") 146 | 147 | // Perform the request 148 | resp, err = app.Test(req, -1) 149 | if err != nil { 150 | t.Fatal(err) 151 | } 152 | 153 | // Check the response status code 154 | if resp.StatusCode != http.StatusOK { 155 | t.Errorf("Expected status code %d but got %d", http.StatusOK, resp.StatusCode) 156 | } 157 | 158 | // Check the response body 159 | expected = `{"message":"0"}` 160 | body, err = io.ReadAll(resp.Body) 161 | if err != nil { 162 | t.Fatal(err) 163 | } 164 | if string(body) != expected { 165 | t.Errorf("Expected response body '%s' but got '%s'", expected, string(body)) 166 | } 167 | 168 | /////////////////////////////////////////////////////////////OCTALTOBİNARY 169 | req = httptest.NewRequest("POST", "/convert", bytes.NewBuffer([]byte(`{ 170 | "value": "60", 171 | "sourceType": "octal", 172 | "destType": "binary" 173 | }`))) 174 | req.Header.Set("Content-Type", "application/json") 175 | 176 | // Perform the request 177 | resp, err = app.Test(req, -1) 178 | if err != nil { 179 | t.Fatal(err) 180 | } 181 | 182 | // Check the response status code 183 | if resp.StatusCode != http.StatusOK { 184 | t.Errorf("Expected status code %d but got %d", http.StatusOK, resp.StatusCode) 185 | } 186 | 187 | // Check the response body 188 | expected = `{"message":"00110000"}` 189 | body, err = io.ReadAll(resp.Body) 190 | if err != nil { 191 | t.Fatal(err) 192 | } 193 | if string(body) != expected { 194 | t.Errorf("Expected response body '%s' but got '%s'", expected, string(body)) 195 | } 196 | 197 | req = httptest.NewRequest("POST", "/convert", bytes.NewBuffer([]byte(`{ 198 | "value": "error", 199 | "sourceType": "ascii", 200 | "destType": "octal" 201 | }`))) 202 | req.Header.Set("Content-Type", "falsecontenttype") 203 | 204 | // Perform the request 205 | resp, err = app.Test(req, -1) 206 | if err != nil { 207 | t.Fatal(err) 208 | } 209 | 210 | // Check the response status code 211 | if resp.StatusCode != http.StatusBadRequest { 212 | t.Errorf("Expected status code %d but got %d", http.StatusOK, resp.StatusCode) 213 | } 214 | 215 | // Check the response body 216 | expected = `{"error":"Bad Request"}` 217 | body, err = io.ReadAll(resp.Body) 218 | if err != nil { 219 | t.Fatal(err) 220 | } 221 | if string(body) != expected { 222 | t.Errorf("Expected response body '%s' but got '%s'", expected, string(body)) 223 | } 224 | } 225 | 226 | func TestMain(t *testing.T) { 227 | // HTTP sunucusu oluşturma 228 | go main() 229 | 230 | // Dinlenmeye hazır olana kadar bekleme 231 | time.Sleep(1 * time.Second) 232 | 233 | reqBody, _ := json.Marshal(map[string]string{ 234 | "value": "test", 235 | "sourceType": "ascii", 236 | "destType": "binary", 237 | }) 238 | 239 | // Sunucuya bir GET isteği gönderme 240 | resp, err := http.Post("http://127.0.0.1:8080/convert", "application/json", bytes.NewBuffer(reqBody)) 241 | if err != nil { 242 | t.Errorf("HTTP isteği gönderirken hata oluştu: %v", err) 243 | } 244 | defer resp.Body.Close() 245 | 246 | // Cevabın 200 OK kodu döndürdüğünü doğrulama 247 | if resp.StatusCode != http.StatusOK { 248 | t.Errorf("Beklenmeyen HTTP durumu kodu: %v", resp.StatusCode) 249 | } 250 | } 251 | --------------------------------------------------------------------------------