Works locally but doesn't pass the tests? (Haskell)

Hello! I’m stuck on Respond with body #CN2

My problem is that when I run it locally the server seems to respond correctly to all the cases so far but when I submit it the tests don’t pass, saying that it got 404 instead of the expected 200. Also it says “try setting ‘debug: true’ in your codecrafters.yml” which I already did but it doesn’t seem to do anything. Thanks in advance! :slight_smile:

Here are the test results:

remote: [tester::#CN2] Running tests for Stage #CN2 (Respond with body)
remote: [tester::#CN2] $ ./your_server.sh
remote: [your_program] Listening on 127.0.0.1:4221
remote: [tester::#CN2] $ curl -v http://localhost:4221/echo/strawberry
remote: [tester::#CN2] Expected status code 200, got 404
remote: [your_program] Accepted connection from 127.0.0.1:44652.
remote: [tester::#CN2] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)

Here is what I get locally running the same test:

curl -v http://localhost:4221/echo/strawberry
DETALLADO: GET http://localhost:4221/echo/strawberry with 0-byte payload
DETALLADO: received 10-byte response of content type text/plain


StatusCode        : 200
StatusDescription : OK
Content           : strawberry
RawContent        : HTTP/1.1 200 OK
                    Content-Length: 10
                    Content-Type: text/plain

                    strawberry
Forms             : {}
Headers           : {[Content-Length, 10], [Content-Type, text/plain]}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 10

And here is a snippet of my code

--Main module snippet
    forever $ do

        (clientSocket, clientAddr) <- accept serverSocket

        BC.putStrLn $ "Accepted connection from " <> BC.pack (show clientAddr) <> "."

        requestData <- recv clientSocket 4096

        BC.putStrLn $ "Received: " <> requestData

        let response = respondRequest $ parseHttpRequest requestData

        BC.putStrLn $ "Responded with: " <> response <> "\n--End of response--"

        -- Handle the clientSocket as needed...
        _ <- send clientSocket response

        close clientSocket
module HttpRequest (respondRequest, parseHttpRequest) where

import qualified Data.ByteString.Char8 as BC
import Data.ByteString (ByteString)

data HttpRequest = HttpRequest { status :: HttpStatus }

data HttpStatus = HttpStatus { method :: ByteString
                             , path :: ByteString
                             , version :: ByteString
                             }
--data HttpBody = HttpBody {}

parseHttpRequest :: ByteString -> HttpRequest
parseHttpRequest raw =
  let (m:p:v:_) = BC.words . head $ BC.lines raw
  in HttpRequest (HttpStatus m p v)

respondRequest :: HttpRequest -> ByteString
respondRequest req =
    case path (status req) of
        "/" -> "HTTP/1.1 200 OK\r\n\r\n"
        p | "/echo/" `BC.isPrefixOf` p -> echoRespond (status req)
        _ -> "HTTP/1.1 404 Not Found\r\n\r\n"

echoRespond :: HttpStatus -> ByteString
echoRespond httpStatus = 
     "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: "<> echoLength <> "\r\n\r\n" <> echo
     where
        echo = BC.drop 6 (path httpStatus)
        echoLength = BC.pack . show $ BC.length (path httpStatus) - 6 --Subtract /echo/

Hmm, @ShyManifold when you turn debug: true you should see more details (the exact request sent + the response received). Are you using the CLI or Git? If you’re using Git, note that the changes to codecrafters.yml need to be committed for the test runner to pick them up.

1 Like

Thanks for that key insight! I am using git but I wasn’t committing my changes just running

git add .
git commit --allow-empty -m "pass stage"
git push origin masterts

on the terminal. After committing it passed all the tests.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.