Repository: actix/actix-web
File: actix-files/src/lib.rs (Rust)

Code before:
        let req = TestRequest::default().uri("/tests").to_srv_request();
        let resp = test::call_service(&service, req).await;
        assert_eq!(
            resp.headers().get(header::CONTENT_TYPE).unwrap(),
            "text/html; charset=utf-8"
        );
        let bytes = test::read_body(resp).await;
        assert!(format!("{:?}", bytes).contains("/tests/test.png"));
    }

    #[actix_rt::test]
    async fn test_path_filter() {
        // prevent searching subdirectories
        let st = Files::new("/", ".")
            .path_filter(|path, _| path.components().count() == 1)
            .new_service(())
            .await
            .unwrap();

        let req = TestRequest::with_uri("/Cargo.toml").to_srv_request();
        let resp = test::call_service(&st, req).await;
        assert_eq!(resp.status(), StatusCode::OK);

        let req = TestRequest::with_uri("/tests/test.png").to_srv_request();
        let resp = test::call_service(&st, req).await;
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[actix_rt::test]
    async fn test_default_handler_filter() {
        let st = Files::new("/", ".")
            .default_handler(|req: ServiceRequest| {
                ok(req.into_response(HttpResponse::Ok().body("default content")))
            })
            .path_filter(|path, _| path.extension() == Some("png".as_ref()))
            .new_service(())
            .await
            .unwrap();
        let req = TestRequest::with_uri("/Cargo.toml").to_srv_request();
        let resp = test::call_service(&st, req).await;

        assert_eq!(resp.status(), StatusCode::OK);
        let bytes = test::read_body(resp).await;
        assert_eq!(bytes, web::Bytes::from_static(b"default content"));
    }
}


Review comment (suggestion):
This test doesn't really prove that the filter works, it would be the same outcome if the file really wasnt there. ~If we set a `.default_handler()` that returns a different status code then the test would be valid.~

Code after:
        let req = TestRequest::default().uri("/tests").to_srv_request();
        let resp = test::call_service(&service, req).await;
        assert_eq!(
            resp.headers().get(header::CONTENT_TYPE).unwrap(),
            "text/html; charset=utf-8"
        );
        let bytes = test::read_body(resp).await;
        assert!(format!("{:?}", bytes).contains("/tests/test.png"));
    }

    #[actix_rt::test]
    async fn test_path_filter() {
        // prevent searching subdirectories
        let st = Files::new("/", ".")
            .path_filter(|path, _| path.components().count() == 1)
            .new_service(())
            .await
            .unwrap();

        let req = TestRequest::with_uri("/Cargo.toml").to_srv_request();
        let resp = test::call_service(&st, req).await;
        assert_eq!(resp.status(), StatusCode::OK);

        let req = TestRequest::with_uri("/src/lib.rs").to_srv_request();
        let resp = test::call_service(&st, req).await;
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[actix_rt::test]
    async fn test_default_handler_filter() {
        let st = Files::new("/", ".")
            .default_handler(|req: ServiceRequest| {
                ok(req.into_response(HttpResponse::Ok().body("default content")))
            })
            .path_filter(|path, _| path.extension() == Some("png".as_ref()))
            .new_service(())
            .await
            .unwrap();
        let req = TestRequest::with_uri("/Cargo.toml").to_srv_request();
        let resp = test::call_service(&st, req).await;

        assert_eq!(resp.status(), StatusCode::OK);
        let bytes = test::read_body(resp).await;
        assert_eq!(bytes, web::Bytes::from_static(b"default content"));
    }
}