ajaynegi45 / Http-Server

A lightweight, custom HTTP server built in Java that handles basic GET requests and supports multithreaded request handling. It’s easy to extend for additional HTTP methods and ideal for learning the fundamentals of server-side programming.
7 stars 15 forks source link

Write Comprehensive Unit Tests to Achieve 100% Code Coverage #12

Closed ajaynegi45 closed 3 weeks ago

ajaynegi45 commented 1 month ago

Problem Statement:

Ensuring that our HTTP server is fully reliable and scalable requires having a solid suite of tests that cover every possible edge case, flow, and functionality. Currently, our codebase lacks detailed unit tests, and without these, we risk introducing undetected bugs and regressions when we extend or modify the code. In order to move towards an industry-ready, highly scalable, and secure server, we need to ensure 100% code coverage by writing thorough and comprehensive unit tests.

Why This Matters:

Your Contribution:

We’re looking for contributors to help write detailed unit tests that cover 100% of the codebase. Each critical path, error state, and edge case needs to be tested. These tests will not only improve the stability and scalability of the server but also prepare us for advanced implementations like asynchronous I/O, better security features, and future enhancements.

Guidelines for Contribution:

  1. Implementation Plan: If you're interested in working on this issue, please comment below with a detailed plan on how you intend to write the tests. Your plan should include:

    • The specific classes/modules you’ll be testing.
    • A list of all edge cases, success cases, and failure cases you plan to cover.
    • How you will handle mocking/stubbing (if needed).
    • Tools or libraries you will use for testing (e.g., JUnit, Mockito, etc.).
    • How you’ll measure code coverage (tools like JaCoCo or IntelliJ’s built-in coverage tools are acceptable).
  2. Read Project Files:

    • Before starting, make sure to thoroughly read the README.md file to understand the project's architecture, goals, and purpose. This will give you a clear understanding of our mission and the functionality you’ll be testing.
    • Review the CONTRIBUTING.md file carefully to understand how to properly contribute to this repository. It includes essential guidelines that make contributing smoother, especially for newcomers.
  3. Unit Test Requirements:

    • Write unit tests for every possible scenario — no edge case is too small.
    • Ensure tests are written for:
      • All public methods (positive and negative test cases).
      • Error handling (e.g., invalid HTTP methods, malformed requests).
      • Boundary conditions and edge cases (e.g., handling large requests, incorrect request headers).
      • Exception flows (such as handling BadHttpVersionException, HttpParsingException, etc.).
    • Strive for clear, descriptive test names that explain what is being tested and why.
  4. Testing Framework: We prefer using:

    • JUnit: For basic unit testing.
    • Mockito: For mocking dependencies where needed.
    • JaCoCo: For tracking code coverage. Ensure that after running tests, the coverage is close to or at 100%.

Next Steps:

  1. Comment with your plan before starting any work. We want to ensure that everyone’s approach aligns with our project’s goals.
  2. Once your plan is approved, you can start working on the implementation.

Note: All contributions should be in accordance with our CONTRIBUTING.md guidelines. We also encourage contributors to break down their work into small, manageable pull requests. This allows for easier review and testing.

Thank you for helping us make this project stronger, more reliable, and ready for real-world, high-demand environments! We look forward to your contributions and can't wait to see the innovative testing approaches you bring to the table. 😊


Additional Tips:

Ready to Contribute? 🎉 Let’s make this server bulletproof with robust testing!

Fl1s commented 1 month ago

Good evening, I'd like to solve this issue. Here's my cool plan:

  1. I will cover the http, https directory, as well as exception and json.

2.1. [HttpConnectionWorkerThread] • Malformed Requests: Handle cases where the HTTP request does not conform to expected formats (e.g., missing method or headers). • Timeouts: Consider scenarios where the client takes too long to send data. Implement read timeouts for socket operations. • Invalid Headers: Requests may contain incorrect or missing headers. Make sure to return appropriate error codes (e.g., 400 Bad Request). • Excessively Long URIs: Define a maximum length for URls and handle cases where this limit is exceeded. • Socket Overflow: Handle scenarios where the input stream of the socket gets filled with too much data. • Closing Connection Errors: Handle potential errors that may occur when closing the socket after request processing.

2.2. [HttpServerListenerThread] • Max Connection Limit: Manage the number of simultaneous connections being processed. Implement a denial of service mechanism when the max thread count is reached. • Memory Allocation Issues: If the server spawns too many threads, resource exhaustion could occur. Limit the number of threads that can be created. • Server Startup Errors: Catch exceptions when trying to bind the socket to a port, and inform about the server being unable to start (e.g., if the port is already in use).

2.3. [HttpsConnectionWorkerThread] • Invalid Certificates: Handle situations where the client attempts to connect with an invalid or expired certificate. • Encryption Problems: Ensure the server correctly manages cases where the client does not support the required encryption or SSL/TLS protocol. • Data Reading Errors: Handle exceptions that may arise when reading data from the SSL socket's input stream.

2.4. [HttpsServerListenerThread] • Failed SSL Initialization: Handle errors during SSL server setup, such as missing or incorrectly configured certificate files. • Invalid Configuration Parameters: Ensure SSL context parameters are correct to avoid silent failures during connection handling. • Outdated Protocol Handling: Manage situations where the client tries to use deprecated SSL/TLS versions.

^^^ I may have gotten very convoluted, but I would basically calculate every such scenario.

  1. As usual
  2. I use JUnit and Mockito
  3. I prefer Coverage, but can use JaCoCo
ajaynegi45 commented 1 month ago

Good evening, I'd like to solve this issue. Here's my cool plan:

  1. I will cover the http, https directory, as well as exception and json.

2.1. [HttpConnectionWorkerThread] • Malformed Requests: Handle cases where the HTTP request does not conform to expected formats (e.g., missing method or headers). • Timeouts: Consider scenarios where the client takes too long to send data. Implement read timeouts for socket operations. • Invalid Headers: Requests may contain incorrect or missing headers. Make sure to return appropriate error codes (e.g., 400 Bad Request). • Excessively Long URIs: Define a maximum length for URls and handle cases where this limit is exceeded. • Socket Overflow: Handle scenarios where the input stream of the socket gets filled with too much data. • Closing Connection Errors: Handle potential errors that may occur when closing the socket after request processing.

2.2. [HttpServerListenerThread] • Max Connection Limit: Manage the number of simultaneous connections being processed. Implement a denial of service mechanism when the max thread count is reached. • Memory Allocation Issues: If the server spawns too many threads, resource exhaustion could occur. Limit the number of threads that can be created. • Server Startup Errors: Catch exceptions when trying to bind the socket to a port, and inform about the server being unable to start (e.g., if the port is already in use).

2.3. [HttpsConnectionWorkerThread] • Invalid Certificates: Handle situations where the client attempts to connect with an invalid or expired certificate. • Encryption Problems: Ensure the server correctly manages cases where the client does not support the required encryption or SSL/TLS protocol. • Data Reading Errors: Handle exceptions that may arise when reading data from the SSL socket's input stream.

2.4. [HttpsServerListenerThread] • Failed SSL Initialization: Handle errors during SSL server setup, such as missing or incorrectly configured certificate files. • Invalid Configuration Parameters: Ensure SSL context parameters are correct to avoid silent failures during connection handling. • Outdated Protocol Handling: Manage situations where the client tries to use deprecated SSL/TLS versions.

^^^ I may have gotten very convoluted, but I would basically calculate every such scenario. 3. As usual 4. I use JUnit and Mockito 5. I prefer Coverage, but can use JaCoCo

Hi @Fl1s,

Thank you for expressing your interest in working on this issue. I'm delighted to inform you that I have assigned this issue to you. Your detailed plan looks promising, especially the way you intend to cover critical components like the HttpConnectionWorkerThread, HttpServerListenerThread, and their HTTPS counterparts. Your thorough approach to handling various edge cases such as malformed requests, socket overflow, and encryption problems is exactly what we need to ensure a robust server.

Before making any contributions, please take a moment to carefully read the README.md file to gain a better understanding of the project's goals and purpose. This will give you clarity on our mission. Also, make sure to review the CONTRIBUTING.md file, which contains important guidelines to make the contribution process smoother, especially for newcomers.

Feel free to start working, and if you have any questions or need assistance during the process, please don't hesitate to reach out. We’re excited to see your contribution!

Best of luck!

Fl1s commented 1 month ago

Hey, @ajaynegi45. Can you review my pull-request? #19